使用多个搜索项查找两个列表的交集

时间:2015-03-18 20:44:44

标签: c# list intersection

我有以下对象(以JSON格式显示)

 UserBatch1{
       User{ name: user1; jobtitle: admin; department: finance; location: building1; extn: 1234}
       User{ name: user2; jobtitle: techie; department: engineering; location: building2; extn: 4321}
       User{ name: user3; jobtitle:boss; department:management: location: building3; extn: 5555}
}
UserBatch2{
       User{ name: user1; jobtitle: admin; department: finance; location: buildlig1 extn: 1234}
       User{ name: user4; jobtitle: techie; department: manufacturing; location: building4; extn: 8888}
       User{ name: user5; jobtitle: admin; department: management; location: building1; extn: 7777}
}

对象包含在列表中,例如

List<User> UserBatch1;
List<User> UserBatch2;

如何使用C#找到这些列表的交集,例如,name包含“user”AND jobtitle =“admin”。
另一个例子是department = finance AND jobtitle = admin AND location = building1 为了清晰起见,我简化了问题,但每个对象都有10个属性,我将有10个列表。任何属性组合都可以用作搜索条件。

1 个答案:

答案 0 :(得分:1)

var User = UserBatch1.FirstOfDefault(i => i.department == "finance" && i.jobtitle = "admin");