Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialAaron Selonke
10,323 PointsWhy use the Join() to check for duplicates
Why did Carling use the Join to check for duplicate birds between the two Lists? Why not use the Set Operators? Like Union() ?
3 Answers
Steven Parker
231,110 PointsAn inner join will only return rows where the key fields match. This makes it perfect for finding duplicates.
A Union will return all the unique rows from both sets, effectively concealing any duplication.
Samuel Ferree
31,722 PointsIs there some reason we couldn't just add the birds with:
//Add imported birds, where the list of bird names does not contain their name
birds.AddRange(importedBirds.Where(ib => !birds.Select(b => b.CommonName).Contains(ib.CommonName)));
Seems like this Joining and Flattening and new anonymous datatypes not really necessary.
Luis Marsano
21,425 PointsCould be less efficient if birds.Select(b => b.CommonName)
is recomputed for each item of importedBirds
.
However, just replacing an expression with its value (var birdNames = birds.Select(b => b.CommonName);
and !birdNames.Contains(ib.CommonName)
) eliminates recomputation and yields the same result (due to referential transparency discussed in an earlier video about functional programming).
Your code is simpler and clearer (as good code should be) by decomposing a problem into simpler logical expressions and composing together in a straightforward fashion.
That's the beauty of functional programming.
Elshad Shabanov
2,890 PointsWhy we don't use Except for defining birds from imported birds list which are not in our birds list? Like this:
importedBirds.Except(birds).Distinct();
Patrick Castle
Courses Plus Student 14,591 PointsThis can work but only if you provide additional classes that tell the compiler to compare two Bird objects against one or more of its properties. Without doing this, the unique identifier of each individual object instance will be used for comparison and in this case that means that all instances are unique.
The documentation here will provide more info on this: https://msdn.microsoft.com/en-us/library/bb300779(v=vs.110).aspx