ReneK
ReneK

Reputation: 109

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.

Upvotes: 0

Views: 87

Answers (2)

citronas
citronas

Reputation: 19365

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

Upvotes: 1

InbetweenWeekends
InbetweenWeekends

Reputation: 1414

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

Upvotes: 0

Related Questions