Reputation: 1396
I have a list of cars. Each car has a number of doors. And each door has a color. I want to iterate over the list and select only those cars that have more than one door of a matching color.
I've tried something like this but it's not working like I expect it to:
foreach (Car car in cars.Where(c=>c.doors.Select(d=>d.color = "Blue").Count() > 1))
{
// Do something
}
Can you help me understand what I'm doing wrong?
Upvotes: 1
Views: 289
Reputation: 8449
You use c.color = "blue"
which is assignment.
you should use c.color == "blue"
or color.Equals("blue")
for comparison.
Upvotes: 0
Reputation: 38478
You need to replace Select with Where and pass a predicate:
foreach (Car car in cars.Where(c=>c.doors.Where(d=>d.color == "Blue").Count() > 1))
To make it shorter:
foreach (Car car in cars.Where(c=>c.doors.Count(d=>d.color == "Blue") > 1))
Upvotes: 3