Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

C# Querying With LINQ Querying the BirdWatcher Data Bird Importing

Aaron Selonke
Aaron Selonke
10,323 Points

Why 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
Steven Parker
221,326 Points

An 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.
:sparkles:

Samuel Ferree
Samuel Ferree
31,721 Points

Is 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.

Could 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
Elshad Shabanov
2,890 Points

Why 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();

This 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