我开始喜欢Lambda的表达,但我正在努力通过这面墙:
public class CompanyWithEmployees { public CompanyWithEmployees() { } public Company CompanyInfo { get; set; } public ListEmployees { get; set; } }
我的搜索:
ListcompaniesWithEmployees = ws.GetCompaniesWithEmployees(); CompanyWithEmployees ces = companiesWithEmployees .Find(x => x.Employees .Find(y => y.PersonID == person.PersonID));
所以,我想获得具有我正在寻找的那个Person(Employee)的Object"CompanyWithEmployees",但我得到" 不能隐含地将'Person'转换为'bool') "这是正确的,但如果我'我没有传递Person对象,第一个Find怎么执行?
因为你想检查是否存在,或许尝试:
ces = companiesWithEmployees .Find(x => x.Employees .Find(y => y.ParID == person.ParID) != null);
这将检查任何Person
相同的ParID
; 如果你的意思是相同的Person
实例(参考),那么Contains
就足够了:
ces = companiesWithEmployees .Find(x => x.Employees.Contains(person));
Find()
返回找到的对象.用于Any()
检查表达式是否适用于任何元素.
var ces = companiesWithEmployees .Find(x => x.Employees .Any(y => y.PersonID == person.PersonID));