gpt4 book ai didi

c# - 具有嵌套类的 Lambda

转载 作者:太空狗 更新时间:2023-10-29 21:55:40 26 4
gpt4 key购买 nike

我刚才发布了这个问题,但我的问题得到了部分答案,所以我想我发布更多解释,希望得到更准确的答案。我有 2 个类(class):

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();

但是,如果至少有一辆货车分配给一名员工 (.Any),这会得到所有员工,如果我尝试 (.All),它不会带回任何东西,因为不是所有员工都有面包车。

知道这是否可以使用嵌套的 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 });

最佳答案

试试这个:

List<Employee> Temp = AllEmployees.Where(emps => emps.Cars.Any(cartype => cartype.CarType == Cars.CarTypes.Van)).ToList();

List<Employee> EmployeesWithVans = (from item in Temp
select new Employee{
Name = item.Name,
Cars = (item.Cars.Where( car => car.CarType == Cars.CarTypes.Van)).ToList()
}).ToList();

这是我尝试过的(在 LINQPAD 中):

void Main()
{
List<Employee> AllEmployees = new List<Employee>();

List<Cars> lcars1 = new List<Cars>();
Cars car1 = new Cars();
car1.CarType = Cars.CarTypes.Van;
lcars1.Add(car1);lcars1.Add(car1);

Cars car2 = new Cars();
car2.CarType = Cars.CarTypes.SmallCar;
lcars1.Add(car2);

List<Cars> lcars2 = new List<Cars>();
lcars2.Add(car1);lcars2.Add(car2);lcars2.Add(car2);

AllEmployees.Add(new Employee(){ Name="emp1", Cars = lcars1});
AllEmployees.Add(new Employee(){ Name="emp2", Cars = lcars2});
AllEmployees.Add(new Employee(){ Name="emp3", Cars = lcars1 });
AllEmployees.Add(new Employee(){ Name="emp4", Cars = lcars2});

List<Employee> Temp = AllEmployees.Where(emps => emps.Cars.Any(cartype => cartype.CarType == Cars.CarTypes.Van)).ToList();

List<Employee> EmployeesWithVans = (from item in Temp
select new Employee{
Name = item.Name,
Cars = (item.Cars.Where( car => car.CarType == Cars.CarTypes.Van)).ToList()
}).ToList();

EmployeesWithVans.Dump();
}

输出:

enter image description here

关于c# - 具有嵌套类的 Lambda,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5130152/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com