2011-02-26 61 views
5

前段時間我發佈了這個問題,但得到了我的問題的部分答案,所以我想我發佈更多的解釋希望得到更準確的答案。我有2類:帶嵌套類的Lambda

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

但是,這得到所有員工如果我嘗試(.All),如果至少有一輛麪包車被分配給員工(.Any),則它不會帶來任何回報,因爲並非所有員工都有範。

任何想法,如果這可以實現使用嵌套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 }); 
+0

看起來像她想僱員誰也不會有一個小型車... –

+0

你還沒有提到'carID'等。但我已經編輯我的答案,根據您的更新.. –

回答

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

+0

這將返回所有員工誰**不**有一輛麪包車 - 我不認爲這是OP想要的 – BrokenGlass

+0

是的,我只是想它,並發現..已經改變了我的代碼.. Thanx指出, –

+0

@maya:我已經更新我的答案根據你的更新 –

0

這是你想要的嗎? (至少有一個麪包車,但沒有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(); 
0

查詢是正確的,如果它被賦予了一輛麪包車它添加一個Employee到返回序列。我不太確定問題出在哪裏,您是否認爲您的查詢將不會返回,因爲並非所有員工都有面包車?如果是的話,這將是一個Where操作執行:

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

謂詞將在您的順序應用於所有元素,如果元素滿足它,它會返回作爲序列的一部分。

+0

@BrokenGlass @Femaref @Shekhar_Pro我已經添加了一個問題的例子,它是否有意義,輸出一個新的員工列表與員工項目包含類型麪包車只有添加? – Maya