SQL Linq包含列表中的项目

时间:2014-07-10 12:25:23

标签: c# linq

这是我的代码:

CommissionCalculationDataContext dc = new CommissionCalculationDataContext();
CrossAppDataContext dc2 = new CrossAppDataContext();
List<Group_ADUser_Mapping> mapList = (from a in dc.Group_ADUser_Mappings where a.GroupID == '3' select a).ToList();
List<ADUser> userList = (from a in dc2.ADUsers where mapList.Contains(a.sAMAccountName) select a).ToList();  //doesnt work

目标是,我有一个映射列表,然后只需要获取此映射列表中的用户(通过SAMAccountName)

错误是:OVerload问题....

所以实际上我想将Group_ADUser_Mappings.sAMAccountNameADUser.sAMAAccountName进行比较。

2 个答案:

答案 0 :(得分:1)

尝试

List<String> mapList = dc.Group_ADUser_Mappings.Where(x =>  x.GroupID == '3').Select(y => y.sAMAccountName).ToList();
List<ADUser> userList = dc2.ADUsers.Where(x => mapList.Contains(x.sAMAccountName)).ToList();

为了避免List.Contains的大/小写问题,您可以在之后进行过滤。这应该工作(未经测试):

userList = userList.Where(x => mapList.Any(y => y.sAMAccountName == x.sAMAccountName)).ToList();

答案 1 :(得分:0)

您可以加入这两个实体并选择您的ADUser吗?

List<ADUser> userList = (from a in dc2.ADUsers 
    join b in dc.Group_ADUser_Mappings  on a.sAMAccountName equals b.sAMAccountName 
    where b.GroupID == "3" 
    select a).ToList();