假设我有以下对象:
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?
}
如何找到此方法的输入标题中至少有一个标题的人员列表?
答案 0 :(得分: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
,否则无法编译。