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 Aggregates

help plz

Challenge Task 1 of 2

Create a variable named sumOfSightings and assign it to the sum of all the Sightings in the ourBirds list. Bummer! Your code could not be compiled. Please click on "Preview" to view the compiler errors. Restart Preview Get Help Recheck work CodeChallenge.cs

1 var myBirds = new List<Bird> 2 { 3 new Bird { Name = "Cardinal", Color = "Red", Sightings = 3 }, 4 new Bird { Name = "Dove", Color = "White", Sightings = 2 }, 5 new Bird { Name = "Robin", Color = "Red", Sightings = 5 } 6 }; 7 ā€‹ 8 var yourBirds = new List<Bird> 9 { 10 new Bird { Name = "Dove", Color = "White", Sightings = 2 }, 11 new Bird { Name = "Robin", Color = "Red", Sightings = 5 }, 12 new Bird { Name = "Canary", Color = "Yellow", Sightings = 0 } 13 }; 14 ā€‹ 15 var ourBirds = myBirds.Join(yourBirds, 16 mb => mb.Name, 17 yb => yb.Name, 18 (mb, yb) => (mb)); 19 var sumOfSightings.Sum( b => b.Sightings);

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,
                            mb => mb.Name,
                            yb => yb.Name,
                            (mb, yb) => (mb));
var sumOfSightings.Sum( b => b.Sightings);

2 Answers

Roman Fincher
Roman Fincher
18,267 Points

Looks like you're taking the sum for the sumOfSightings variable, which you haven't yet defined.

Instead you should be trying to assign the sum of the sightings in the variable ourBirds to a new sumOfSightings variable, which you instantiate as the result of the query to ourBirds.

{ 
    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,
                            mb => mb.Name,
                            yb => yb.Name,
                            (mb, yb) => (mb));

var sumOfSightings = ourBirds.Sum(b => b.Sightings);