带有嵌套类的 Lambda

发布于 2024-10-19 04:11:36 字数 1638 浏览 3 评论 0原文

我不久前发布了这个问题,但得到了我的问题的部分答案,所以我想我发布更多解释希望得到更准确的答案。我有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

花间憩 2024-10-26 04:11:36

试试这个:

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

输出:

在此处输入图像描述

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:

enter image description here

浴红衣 2024-10-26 04:11:36

这是你想要的吗? (所有员工至少拥有一辆货车,但没有小型车)

var EmployeesWithVans = AllEmployees
                        .Where(emps => emps.Cars.Any(cartype => cartype.CarType == Cars.CarTypes.Van)
                                       && !emps.Cars.Any(cartype => cartype.CarType == Cars.CarTypes.SmallCar))
                        .ToList();

Is this what you want? (all employees with at least one van, but no smallcar)

var EmployeesWithVans = AllEmployees
                        .Where(emps => emps.Cars.Any(cartype => cartype.CarType == Cars.CarTypes.Van)
                                       && !emps.Cars.Any(cartype => cartype.CarType == Cars.CarTypes.SmallCar))
                        .ToList();
撑一把青伞 2024-10-26 04:11:36

查询是正确的,如果分配了货车,它会将 Employee 添加到返回序列中。我不太确定问题出在哪里,您认为您的查询不会返回任何内容,因为并非所有员工都有货车?如果是,这将是 Where 运算符的实现:

foreach(var elem in input)
{
  if (predicate(elem))
    yield return elem;
}

谓词将应用于序列中的所有元素,如果元素满足它,它将作为序列的一部分返回。

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 a Where operator:

foreach(var elem in input)
{
  if (predicate(elem))
    yield return elem;
}

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文