Lambda表达式,如何在对象内搜索?

时间:2009-01-06 08:59:04

标签: c# 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));

所以,我希望得到具有我正在寻找的Person(Employee)的Object“CompanyWithEmployees”,但我得到“无法隐式转换'Person'为'bool')“哪个是正确的,但是如果我没有传递Person对象,那么第一个Find怎么执行?

6 个答案:

答案 0 :(得分:14)

因为您想检查是否存在,请尝试:

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

答案 1 :(得分:7)

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

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

答案 2 :(得分:3)

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

答案 3 :(得分: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; }
}

答案 4 :(得分:0)

那是因为您没有为顶级查找指定合法的查找表达式。

我会在这里显示:

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

那么你初步找到的条件是什么?

答案 5 :(得分:0)

最简单的就是

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

没有任何空检查