简体   繁体   中英

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)

Error is: Has OVerload problems....

So in fact I want to compare Group_ADUser_Mappings.sAMAccountName with ADUser.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. 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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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