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,009 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);