C# Querying With LINQ Query Operators Quantifiers

Jorleni Maldonado
Jorleni Maldonado
14,398 Points

What am I missing?

Please help :)

CodeChallenge.cs
var birds = 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 },
    new Bird { Name =  "Canary", Color = "Yellow", Sightings = 0 },
    new Bird { Name =  "Blue Jay", Color = "Blue", Sightings = 1 },
    new Bird { Name =  "Crow", Color = "Black", Sightings = 11 },
    new Bird { Name =  "Pidgeon", Color = "White", Sightings = 10 }
};

var anyBlueBirds = false; if birds.Any(b=>b.Color == "Blue"); { anyBlueBirds = true; }
var allBlueBirds = false; if birds.All(b=>b.Color == "Blue");  { allBlueBirds = true; }
Jorleni Maldonado
Jorleni Maldonado
14,398 Points

Create a variable named allBlueBirds and write a LINQ query using the All method to assign a boolean value if all of the Bird objects in the birds list have the value "Blue" in the Color property.

1 Answer

Luis Duenas
Luis Duenas
5,894 Points

Both of your answers are wrong, but let me focus in this line

var allBlueBirds = false; if birds.All(b=>b.Color == "Blue");  { allBlueBirds = true; }

here you are saying "set allBlueBirds to false, then evaluate if all birds are blue then set allBlueBirds to true"

var allBlueBirds = false;  // set variable to false
if birds.All(b=>b.Color == "Blue");  // evaluate if all birds are blue but do nothing because of the semicolon
{ allBlueBirds = true; } // set variable to true

when you put the semicolon at the end of the if statement, the code inside the following brackets doesn't care for that evaluation, also, as shown in the videos, you can simply assing the value of the linq expression to your variable like this:

var anyBlueBirds = birds.Any(b => b.Color == "Blue");
var allBlueBirds = birds.All(b => b.Color == "Blue");