tmoore82
tmoore82

Reputation: 1875

Select All from Collection Where ID in List

I'm just learning to use lambda expressions and was turned on to Dapper a couple days ago. I'm working on an app that populates a listView with usernames to be selected for some account maintenance. I'm having trouble with one part of sorting:

To populate the listView with only the users who haven't been migrated, I need to select only those users without q.salesIDs in migrated. I just can't quite figure out if/how I can do this without using some kind of foreach. Seems like there should be a way to select it.

Granted, I'm looping through uniqUsers anyway, and populating the listView with values out of each object's properties. I can add a statement to check if the current ID is in migrated, but my gut is just telling me that I can do it with the select statement and save myself a step.

I'm thinking something like this:

 var  uniqUsers = Global.allUsers.Where(i => i.salesIDs not in migrated).OrderBy(n => n.lastNames).GroupBy(q => q.salesIDs);

but i => i.salesIDs not in migrated isn't cutting it.

Upvotes: 4

Views: 3651

Answers (1)

DGibbs
DGibbs

Reputation: 14618

This does what you need:

var uniqUsers = allUsers.Where(x => migrated.Contains(x.salesIDs))
                        .OrderBy(y => y.lastNames)
                        .GroupBy(z => z.salesIDs).SelectMany(v => v).ToList();

Gets all users where the salesID is in migrated, then orders by lastNames and then groups by the salesID. The SelectMany() will project the elements to a sequence, the ToList() will enumerate the results.

Upvotes: 4

Related Questions