2013-08-12 169 views
2
public class Employee 
    { 
     public string Name{get;set;} 
     public List<Department> Department { get; set; } 
     public string Company{get;set;} 
    } 

    public class Department 
    { 
     public string Name { get; set; } 
     public string Location { get; set; } 
    } 

    List<Employee> employees = new List<Employee>(); 
     employees.Add(new Employee() { Company = "Dell", Name = "ABC" }); 
     employees.Add(new Employee() { Company = "Dell", Name = "Aakash" }); 
     employees.Add(new Employee() { Company = "CSC", Name = "Vaibhav" }); 
     employees.Add(new Employee() { Company = "TCS", Name = "Sambhav" }); 

     employees[0].Department = new List<Department>(); 
     employees[0].Department.Add(new Department() { Location = "Delhi", Name = "HR" }); 
     employees[0].Department.Add(new Department() { Location = "Delhi", Name = "Recruitment" }); 

     employees[1].Department = new List<Department>(); 
     employees[1].Department.Add(new Department() { Location = "Noida", Name = "EAO" }); 
     employees[1].Department.Add(new Department() { Location = "Delhi", Name = "Arch" }); 

     employees[2].Department = new List<Department>(); 
     employees[2].Department.Add(new Department() { Location = "Denmark", Name = "Scandi" }); 
     employees[2].Department.Add(new Department() { Location = "Noida", Name = "SAG" }); 

     employees[3].Department = new List<Department>(); 
     employees[3].Department.Add(new Department() { Location = "Mumbai", Name = "NSE" }); 

我需要寫一個lambda表達式來選擇所有員工,其中DEPARTEMENT位置是諾伊達LINQ的名單列表

回答

7

使用Any擴展方法:

var results = employees.Where(e => e.Department.Any(d => d.Location == "Noida")) 
         .ToList(); 
+0

不知道'ToList()'是必要的,但+1。 – Jodrell

+0

如何找到Deptartment Name的唯一值。 –

+0

還有其他問題?問另一個問題。 – MarcinJuraszek

1

其實你甚至不需要LINQ。 List<T>有方法FindAllExsists可以做的工作:

employees.FindAll(e => e.Department.Exists(d => d.Location == "Noida"))