2012-02-20 112 views
0

假設我有以下obejct:如何做到這一點的LINQ

class title 
{ 
    string Title; 
    int id; 
    // other information about title 
} 

class person 
{ 
    string Name; 
    List<title> titles; 
    // other information about person 

} 

List<Person> FindPersonsBasedOnTitle(List<Title> titles) 
{ 
    List<Person> p=getPersons(); 

    // How to search P for all persons that have at least one title matched in titles? 
} 

我如何才能找到誰在輸入標題至少一個標題這種方法的人的名單?

回答

4
List<Person> FindPersonsBasedOnTitle(List<Title> titlesToMatch) 
{ 
    List<Person> p=getPersons(); 

    return p.Where(item => item.titles.Any(title => titlesToMatch.Contains(title)) 
      .ToList(); 
} 

注意,這將不會編譯除非titles是公衆person