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.

有帮助吗?

解决方案

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.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top