前段時間我發佈了這個問題,但得到了我的問題的部分答案,所以我想我發佈更多的解釋希望得到更準確的答案。我有2類:帶嵌套類的Lambda
public class Employee
{
public string Name { get; set; }
public List<Cars> Cars { get; set; }
}
public class Car
{
public int CarID { get; set; }
public CarTypes CarType { get; set; }
public enum CarTypes
{
Van,
SmallCar
}
}
我想只得到所有員工有車分配給忽略那些使用LAMBDA SmallCars,我想這行:
List<Employee> EmployeesWithVans = AllEmployees.Where(emps => emps.Car.Any(cartype => cartype.CarType == Car.CarTypes.Van)).ToList();
但是,這得到所有員工如果我嘗試(.All
),如果至少有一輛麪包車被分配給員工(.Any
),則它不會帶來任何回報,因爲並非所有員工都有範。
任何想法,如果這可以實現使用嵌套Lambda?
謝謝。
編輯:
Employee Mark = new Employee();
Mark.Cars.Add(new Car() { CarType = Car.CarTypes.Van, CarID = 12 });
Mark.Cars.Add(new Car() { CarType = Car.CarTypes.Van, CarID = 13 });
Mark.Cars.Add(new Car() { CarType = Car.CarTypes.SmallCar, CarID = 14 });
Employee Lisa = new Employee();
Lisa.Cars.Add(new Car() { CarType = Car.CarTypes.Van, CarID = 15 });
Lisa.Cars.Add(new Car() { CarType = Car.CarTypes.SmallCar, CarID = 16 });
Lisa.Cars.Add(new Car() { CarType = Car.CarTypes.SmallCar, CarID = 17 });
List<Employee> EmployeesWithVans should contain:
Employee FilteredMark contains:
Mark.Cars.Add(new Car() { CarType = Car.CarTypes.Van, CarID = 12 });
Mark.Cars.Add(new Car() { CarType = Car.CarTypes.Van, CarID = 13 });
Employee FilteredLisa contains:
Mark.Cars.Add(new Car() { CarType = Car.CarTypes.Van, CarID = 15 });
看起來像她想僱員誰也不會有一個小型車... –
你還沒有提到'carID'等。但我已經編輯我的答案,根據您的更新.. –