带有嵌套类的 Lambda
我不久前发布了这个问题,但得到了我的问题的部分答案,所以我想我发布更多解释希望得到更准确的答案。我有 2 个类:
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
),它不会返回任何结果,因为并非所有员工都有 Van。
知道是否可以使用嵌套 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 });
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 });
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
试试这个:
这是我尝试过的(在 LINQPAD 中):
输出:
Try this Instead:
This is what i tried (In LINQPAD):
Output:
这是你想要的吗? (所有员工至少拥有一辆货车,但没有小型车)
Is this what you want? (all employees with at least one van, but no smallcar)
查询是正确的,如果分配了货车,它会将
Employee
添加到返回序列中。我不太确定问题出在哪里,您认为您的查询不会返回任何内容,因为并非所有员工都有货车?如果是,这将是Where
运算符的实现:谓词将应用于序列中的所有元素,如果元素满足它,它将作为序列的一部分返回。
The query is correct, it adds an
Employee
to the return sequence if it is assigned a van. I'm not quite sure where the question lies, do you think your query will return nothing as not all employees have a van? If yes, this would be the implementation of aWhere
operator:The predicate will be applied to all elements in your sequence, if the element fulfills it, it will be returned as part of the sequence.