Reputation: 24799
I have the situation where a list must contains at least the values of another list. So imagine we have list A with values 1, 2, 3. This is the list with the required values.
List B has the values 1, 5, 6, 7
List C has the values 1, 2, 3, 4, 7
List D has the values 2, 5, 6
In this situation I only want List C, since this is the only list which contains the values 1, 2 end 3.
I've tried this, but this doesn't work since it is always true:
query = from doc in query
let tagIds = from t in doc.Tags select t.Id
where parameters.TagIds.Except(tagIds).Count() <= parameters.TagIds.Count()
select doc;
And when using this:
query = from doc in query
let tagIds = from t in doc.Tags select t.Id
where !parameters.TagIds.Except(tagIds).Any<int>()
select doc;
I only get the lists where the list matches exactly the 'required' list.
My question is, how can I solve my situation in a Linq 2 SQL query?
Upvotes: 2
Views: 220
Reputation: 47
Try this link
Does .NET have a way to check if List a contains all items in List b?
here you can run the same above method in all available lists.
public static bool ContainsAllItems(List<T> a, List<T> b)
{
return !b.Except(a).Any();
}
This checks whether there are any elements in b which aren't in a - and then inverts the result.
Upvotes: 0
Reputation: 15772
Try
var query = from doc in query
let tagIds = from t in doc.Tags select t.Id
where parameters.All(p => tagIds.Contains(p))
select doc;
Upvotes: 1