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

Daniel Tkach
Daniel Tkach
7,608 Points

I have no idea what I'm doing though I passed the challenge

Hi folks,

the Join method escapes me. I'm passing the challenges, and also trying on visual studio but I seem to be doing it by chance, because I have no idea what I'm doing. For example this challenge I passed it the first time I tried, and while I was writing the key I was mostly GUESSING. I got it right and I said WTF let me try that again and it was harder but I guessed it again. This is what I wrote: var ourBirds = myBirds.Join(yourBirds, mb => mb.Name, yb => yb.Name, (mb, yb) => mb.Name);

I have no idea what this does or what each parameter is, the MSDN is no help either. I feel pretty puzzled. Would anyone care to shed some light?

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. 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 } };

2 Answers

James Churchill
STAFF
James Churchill
Treehouse Teacher

Daniel,

The Join syntax is tricky. Plus, it's not an operator that I use very often, so I can never seem to recall how to use it; I always have to lookup the documentation for it.

Let me try to break this down...

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

1) myBirds is the "outer sequence". This is first of the two collections that you're joining together.

2) yourBirds is the "inner sequence". This is the second of the two collections that you're joining together.

3) mb => mb.Name is the "outer key selector". When joining two collections of objects, the Join operator needs to know what object property value should be used to know when an object in the "outer" collection can be (or should be) joined to an object in the "inner" collection. So, in this case, the Name property is the property that'll be used.

4) yb => yb.Name is the "inner key selector". Just like with the outer key selector, this is the object property that will be used to know when an object in the "inner" collection can be (or should be) joined to an object in the "outer" collection. So, in this case, the Name property is the property that'll be used.

5) (mb, yb) => mb.Name is the "result selector". Joining two collections together produces a new, third collection. The Join operator needs to know how to create items for the resulting collection. Notice that we need to provide two parameters--one parameter for the outer item and another for the inner item. Then we can use the outer and inner items to create a new item for the resulting collection. In this case, we're just return the Name property on the outer item object, but we could any property from either object or a new anonymous object.

I hope this helps.

Thanks ~James

Daniel Tkach
Daniel Tkach
7,608 Points

That was pretty clear James, I appreciate it! Looks simple enough after reading your explanation and letting it rest for a while. Thank you sir.