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 trial

C# Querying With LINQ Query Operators Joins

Ramy Elsaraf
Ramy Elsaraf
7,633 Points

Need assistance

Create a variable named ourBirds and assign to it a LINQ query that is the result of a join from myBirds onto yourBirds using the Name property as the key. Make sure to return the birds that are the same between the two lists

CodeChallenge.cs
var myBirds = new List<Bird> 
{ 
    new Bird { Name = "Cardinal", Color = "Red", Sightings = 3 },
    new Bird { Name =  "Dove", Color = "White", Sightings = 2 },
    new Bird { Name =  "Robin", Color = "Red", Sightings = 5 }
};

var yourBirds = new List<Bird> 
{ 
    new Bird { Name =  "Dove", Color = "White", Sightings = 2 },
    new Bird { Name =  "Robin", Color = "Red", Sightings = 5 },
    new Bird { Name =  "Canary", Color = "Yellow", Sightings = 0 }
};
var ourBirds = myBirds.Join(yourBirds) b=> b.Name , n=> n,  (bird, name) => bird;

3 Answers

William Li
PLUS
William Li
Courses Plus Student 26,868 Points

I can see that your solution is almost correct here, 2 small issues though.

  • myBirds.Join(yourBirds), the .Join() parenthesis is closed right after the yourBirds, this is incorrect, it should've spanned all the way to the end of the line.
  • forgot the .Name property for n.
var ourBirds = myBirds.Join(yourBirds, b=> b.Name , n=> n.Name,  (bird, name) => bird);

hope it helps.

var ourBirds = myBirds.Join(yourBirds,
                        myBirds => myBirds.Name , yourBirds => yourBirds.Name,
                       (myBirds, yourBirds) => myBirds);

This ought to be clearer & more self-documenting.

Your solution seems to be clearer, but it won't compile on the challenge. Any ideas why?