J'ai posté cette question il y a un moment mais j'ai eu une réponse partielle à mon problème, donc j'ai pensé que je posterais plus d'explications dans l'espoir d'obtenir une réponse plus précise. J'ai 2 classes:Lambda avec des classes imbriquées
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
}
}
J'essaie d'obtenir que tous les employés qui ont vans attribués à ignorer ceux qui SmallCars en utilisant Lambda, j'ai essayé cette ligne:
List<Employee> EmployeesWithVans = AllEmployees.Where(emps => emps.Car.Any(cartype => cartype.CarType == Car.CarTypes.Van)).ToList();
Mais cela devient tous les employés si au moins une fourgonnette est attribuée à un employé (.Any
) si j'essaie (.All
) cela ne rapportera rien car tous les employés n'ont pas Van.
Une idée si cela peut être réalisé en utilisant Lambda imbriqué?
Merci.
Edit:
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 });
On dirait qu'elle veut des employés qui n'ont pas aussi une petite voiture ... –
vous n'avez pas mentionné 'carID' etc. mais j'ai édité ma réponse selon votre mise à jour .. –