Let's say I have a few people in a collection "Employees":
Name | Position | Age
------+----------+----
Tom | Manager | 35
Hank | Driver | 38
Harry | Driver | 45
... | ... | ...
Mark | Driver | 30
----------------------
and I want to get all Drivers if there is at least one among them who's older that 40 years old. Could you help me to complete my LINQ?
UPD. I'd like to do this task using a single LINQ and perfomance has no matter. So decision
var allDrivers = Employees.Where(n => n.Position == "Driver").ToList();
return allDrivers.Any(n => n.Age > 40)
? allDrivers
: new List<Employee>();
is good, but I can't mark it as an answer.
In this particular case I need to get Hank, Harry and Mark. Because all they're drivers and Harry is 45 (>40). But if Harry was 39 I would get nothing as a result because in this case all drivers were under 40.