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 Query Operators Aggregates

Tojo Alex
PLUS
Tojo Alex
Courses Plus Student 13,331 Points

I don't know what is wrong

Please could I have an answer for this challenge .

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 = new { Enumerable.Sum<ourBirds>};

2 Answers

Tim Strand
Tim Strand
22,458 Points

You are supposed to sum the sightings in ourBirds using linq. Your current is

var sumOfSightings = new { Enumerable.Sum<ourBirds>};

however linq sum works as this <list>.Sum(); so if we have an object to sum it would work <list>.Sum(o => o.[object key] so if we think of this in terms of our challenge <list> can be replaced with ourBirds since our birds is a list of objects we need to specify the obj key to use in this case Sightings thus we arrive at the below answer.

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