简体   繁体   English

SQL Linq与包含列表中的项目

[英]SQL Linq with contains items from list

here is my code: 这是我的代码:

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

The target is, I got a mapping list and then only need to get the User which are in this mapping list (by SAMAccountName) 目标是,我得到了一个映射列表,然后只需要获取此映射列表中的User(通过SAMAccountName)

Error is: Has OVerload problems.... 错误是:出现OVerload问题...。

So in fact I want to compare Group_ADUser_Mappings.sAMAccountName with ADUser.sAMAAccountName . 因此,实际上我想将Group_ADUser_Mappings.sAMAccountNameADUser.sAMAAccountName进行比较。

Try 尝试

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

To avoid upper/lower case problems of List.Contains , you can filter afterwards. 为了避免List.Contains大小写问题,您可以随后进行过滤。 This should work (untested): 这应该工作(未试用):

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

Can you just join the two entities and select your ADUser? 您可以只加入两个实体并选择您的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();  

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM