2009-01-06 142 views
7

我开始喜欢Lambda表达式,但我努力通过这道墙:Lambda表达式,如何在对象内部进行搜索?

public class CompanyWithEmployees { 
    public CompanyWithEmployees() { } 
    public Company CompanyInfo { get; set; } 
    public List<Person> Employees { get; set; } 
} 

我的搜索:

List<CompanyWithEmployees> companiesWithEmployees = ws.GetCompaniesWithEmployees(); 
CompanyWithEmployees ces = companiesWithEmployees 
     .Find(x => x.Employees 
     .Find(y => y.PersonID == person.PersonID)); 

所以,我想要得到的对象“CompanyWithEmployees”有那个人(员工),我正在寻找,但我得到“不能隐式转换'人'到'布尔')”这是正确的,但如果我没有通过人对象,如何可以第一次查找执行?

回答

12

因为要检查所有脑干,也许尝试:

ces = companiesWithEmployees 
     .Find(x => x.Employees 
     .Find(y => y.ParID == person.ParID) != null); 

这将检查与同ParID任何Person;如果你的意思是相同Person实例(参考),然后Contains应该足够了:

ces = companiesWithEmployees 
     .Find(x => x.Employees.Contains(person)); 
+0

的财产是`ParID `当我发布; -p – 2009-01-06 09:09:37

+0

是的,我必须改变它,因为它是一个内部名称,所以我改成了一个酒吧lic的名称:)我很抱歉,但我明白了:) – balexandre 2009-01-06 09:16:10

3
ces = companiesWithEmployees 
    .First(x => x.Employees.Any(p=>p.PersonID == person.PersonID)); 
0

那是因为你没有指定一个合法查找表达你的顶级查找。

我会在这里表现出来:

ces = companiesWithEmployees 
    .Find (x => x.Employees.Find(y => y.ParID == Person.ParID) /*condition is missing here*/); 

那么,什么是您的初始查找的条件?

7

Find()返回找到的对象。使用Any()来检查表达式对于任何元素是否为真。

var ces = companiesWithEmployees 
    .Find(x => x.Employees 
    .Any(y => y.PersonID == person.PersonID)); 
2
ces = companiesWithEmployees.Find(x => x.Employees.Find(...)); 

.Find返回只有一个对象,x.Employees.Find(..)返回Person

.Find预计布尔参数(即条件的结果),这就是为什么有一个编译器错误,说Cannot implicit convert 'Person' To 'bool'

.Where可以返回多个对象,因此可以通过所有列表进行迭代。

在您的情况下使用.Where.Any的组合。

下面的代码将说明.Where之间的差别,.Find.Any

public partial class Form2 : Form { 
    public Form2() { 
     InitializeComponent(); 
     var companiesWithEmployees = new List<CompanyWithEmployees>() {     
      new CompanyWithEmployees {     
       CompanyInfo = new Company { CompanyName = "Buen" }, 
       Employees = new List<Person>() { 
        new Person { PersonID = 1976, PersonName = "Michael" }, 
        new Person { PersonID = 1982, PersonName = "Mark" }, 
        new Person { PersonID = 1985, PersonName = "Matthew" },        
        new Person { PersonID = 1988, PersonName = "Morris" } 
       } 
      }, 
      new CompanyWithEmployees { 
       CompanyInfo = new Company { CompanyName = "Muhlach" }, 
       Employees = new List<Person>() { 
        new Person { PersonID = 1969, PersonName = "Aga" }, 
        new Person { PersonID = 1971, PersonName = "Nino" }, 
        new Person { PersonID = 1996, PersonName = "Mark" } 
       } 
      }, 
      new CompanyWithEmployees { 
       CompanyInfo = new Company { CompanyName = "Eigenmann" }, 
       Employees = new List<Person>() { 
        new Person { PersonID = 1956, PersonName = "Michael" },       
        new Person { PersonID = 1999, PersonName = "Gabby" } 
       } 
      } 
     }; 

     // just explicitly declared the types (instead of var) so the intent is more obvious 

     IEnumerable<CompanyWithEmployees> whereAreMichaels = companiesWithEmployees 
      .Where(cx => cx.Employees.Any(px => px.PersonName == "Michael")); 

     string michaelsCompanies = string.Join(", ", whereAreMichaels 
      .Select(cx => cx.CompanyInfo.CompanyName).ToArray()); 

     MessageBox.Show("Company(s) with employee Michael : " + michaelsCompanies); 

     Person findAga = companiesWithEmployees 
      .Find(company => company.CompanyInfo.CompanyName == "Muhlach") 
      .Employees.Find(person => person.PersonName == "Aga"); 

     if (findAga != null) 
      MessageBox.Show("Aga's ID : " + findAga.PersonID.ToString()); 
    } 
} 

class CompanyWithEmployees { 
    public Company CompanyInfo { get; set; } 
    public List<Person> Employees { get; set; } 
} 
class Company { 
    public string CompanyName { get; set; } 
} 
class Person { 
    public int PersonID { get; set; } 
    public string PersonName { get; set; } 
} 
0

最简单的一个是

ces = companiesWithEmployees.FirstOrDefault(x => 
      x.Employees.Any(y => y.PersonID == person.ParID)); 

没有任何空检查

相关问题