我在不久前发布的这个问题,但到了我的问题的部分答案,所以我想我发布更多的解释,希望能够得到更准确的答案。我有2类:
公共类员工
{
公共字符串名称{;组; }
公开名单<汽车>汽车{获得;组; }
}
公共类车
{
公众诠释CarID {获得;组; }
公共CarTypes CarType {获得;组; }
公共枚举CarTypes
{
范,
SmallCar
}
}
我试图让只有那些货车分配给忽略那些与使用lambda SmallCars所有的员工,我想这行:
名单,其中,员工> EmployeesWithVans = AllEmployees.Where(环境管理计划=> emps.Car.Any(cartype => cartype.CarType == Car.CarTypes.Van))。了ToList();
但是,这得到所有员工如果至少有一个面包车被分配给员工(。任何
)如果我尝试(。所有
)会带回什么,因为不是所有的员工有范。
任何想法,这是否可以使用嵌套的lambda实现?
感谢。
编辑:
员工马克=新员工();
Mark.Cars.Add(新车(){CarType = Car.CarTypes.Van,CarID = 12});
Mark.Cars.Add(新车(){CarType = Car.CarTypes.Van,CarID = 13});
Mark.Cars.Add(新车(){CarType = Car.CarTypes.SmallCar,CarID = 14});
员工丽莎=新员工();
Lisa.Cars.Add(新车(){CarType = Car.CarTypes.Van,CarID = 15});
Lisa.Cars.Add(新车(){CarType = Car.CarTypes.SmallCar,CarID = 16});
Lisa.Cars.Add(新车(){CarType = Car.CarTypes.SmallCar,CarID = 17});
名单<员工> EmployeesWithVans应包括:
员工FilteredMark包括:
Mark.Cars.Add(新车(){CarType = Car.CarTypes.Van,CarID = 12});
Mark.Cars.Add(新车(){CarType = Car.CarTypes.Van,CarID = 13});
员工FilteredLisa包括:
Mark.Cars.Add(新车(){CarType = Car.CarTypes.Van,CarID = 15});
解决方案
试试这个来代替:
名单,其中,员工>温度= AllEmployees.Where(环境管理计划=> emps.Cars.Any(cartype => cartype.CarType == Cars.CarTypes.Van))了ToList()。
名单<员工> EmployeesWithVans =(从项目的临时
选择新的Employee {
名称= item.Name,
汽车=(item.Cars.Where(汽车=> car.CarType == Cars.CarTypes.Van))。了ToList()
})了ToList()。
这是我的尝试(在LINQPAD):
无效的主要()
{
名单<员工> AllEmployees =新的名单,其中,员工>();
名单<汽车> lcars1 =新的名单,其中,汽车>();
汽车CAR1 =新车();
car1.CarType = Cars.CarTypes.Van;
lcars1.Add(CAR1); lcars1.Add(CAR1);
汽车CAR2 =新车();
car2.CarType = Cars.CarTypes.SmallCar;
lcars1.Add(CAR2);
名单<汽车> lcars2 =新的名单,其中,汽车>();
lcars2.Add(CAR1); lcars2.Add(CAR2); lcars2.Add(CAR2);
AllEmployees.Add(新员工(){名称=EMP1,汽车= lcars1});
AllEmployees.Add(新员工(){名称=EMP2,汽车= lcars2});
AllEmployees.Add(新员工(){名称=EMP3,汽车= lcars1});
AllEmployees.Add(新员工(){名称=emp4,汽车= lcars2});
名单<员工>温度= AllEmployees.Where(环境管理计划=> emps.Cars.Any(cartype => cartype.CarType == Cars.CarTypes.Van))了ToList()。
名单<员工> EmployeesWithVans =(从项目的临时
选择新的Employee {
名称= item.Name,
汽车=(item.Cars.Where(汽车=> car.CarType == Cars.CarTypes.Van))。了ToList()
})了ToList()。
EmployeesWithVans.Dump();
}
输出:
I have posted this question a while ago but got a partial answer to my issue, so I thought I post more explanation hoping to get a more accurate answer. I have 2 classes:
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
}
}
I'm trying to get only All employees that have vans allocated to ignoring those with SmallCars using Lambda, I tried this line:
List<Employee> EmployeesWithVans = AllEmployees.Where(emps => emps.Car.Any(cartype => cartype.CarType == Car.CarTypes.Van)).ToList();
But this gets all employees if at least one van is allocated to an Employee (.Any
) if I try (.All
) it will bring back nothing as not all employees has Van.
Any idea if this can be achieved using nested Lambda?
Thanks.
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 });
解决方案
Try this Instead:
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();
This is what i tried (In 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();
}
Output:
相关推荐
最新文章