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

Linq join query question

I have this code:

join bird in yourBirds on bird.Name equals bird.Name select new { myBirds = bird.Name, yourBirds = bird.Name };

A range variable `bird' has already been declared in this scope

the question is: 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.

need help, thanks

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 =
                from bird in myBirds
                join bird in yourBirds on bird.Name equals bird.Name
                select new { myBirds = bird.Name, yourBirds = bird.Name }; 

2 Answers

Allan Clark
Allan Clark
10,810 Points

The issue is that you are using the same variable name for both of the tables.

bird.Name equals bird.Name

here is where you are telling linq what variable to join on, but linq cant tell which table you are talking about bc they are both named bird.

Thank you!