具有嵌套类的Lambda

时间:2011-02-26 22:32:38

标签: c# linq .net-4.0 lambda nested-class

我刚刚发布了这个问题但得到了我的问题的部分答案,所以我想我发布了更多解释,希望得到更准确的答案。我有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 });

3 个答案:

答案 0 :(得分:2)

试试这个:

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

答案 1 :(得分:0)

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

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

答案 2 :(得分:0)

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

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

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