C# Querying With LINQ Query Operators Aggregates

Tojo Alex
PRO
Tojo Alex
Pro Student 12,430 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,344 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);