Reputation: 19
I have a list, that contains some packages IDs.
List<int> mustHavePackages = GetMustHavePackages();
And a database table
public class Table
{
public int UserID;
public int PackageID;
}
And I want to get from the database, those users that have all packages from the mustHavePackages
list.
For now I have only achieved to get all users, that have at least one package from mustHavePackages
list.
List<int> users =
(from t in db.Table
where mustHavePackages.Contains(t.PackageID)
select t.UserID).ToList();
And now I'm looking for something like ContainsAll or similar.
Any help will be appreciated.
Upvotes: 0
Views: 554
Reputation: 13399
var temp=
(from t in db.Table
group t by t.UserId into g
select new {UserId = g.Key, PackageIds = g.Select(gg=>gg.PackageId)}).ToList();
var users = (from t in temp
where !mustHavePackages.Except(t.PackageIds).Any()
select t.UserId).ToList();
Something like this perhaps. You can add the 2nd part in the first part as well to make one call and not bring extra data, this is to show you the breakdown.
Upvotes: 1