You should not design message tags in an external request; rather, you need to use an internal query that performs an external filter check. (In SQL, we called this a correlated subquery .)
var query = from post in context.Posts where post.Tags.All(tag => tagIds.Contains(tag.TagId)) select post;
Alternative syntax:
var query = context.Posts.Where(post => post.Tags.All(tag => tagIds.Contains(tag.TagId)));
Edit : Correction for clarifying Slaumas . The next version returns messages containing at least all the tags in the tagIds
collection.
var query = from post in context.Posts where tagIds.All(requiredId => post.Tags.Any(tag => tag.TagId == requiredId)) select post;
Alternative syntax:
var query = context.Posts.Where(post => tagIds.All(requiredId => post.Tags.Any(tag => tag.TagId == requiredId)));
Edit 2 : Fixed above for Slauma. Also includes another alternative that makes full use of the query syntax below:
// Project posts from context for which // no Ids from tagIds are not matched // by any tags from post var query = from post in context.Posts where ( // Project Ids from tagIds that are // not matched by any tags from post from requiredId in tagIds where ( // Project tags from post that match requiredId from tag in post.Tags where tag.TagId == requiredId select tag ).Any() == false select requiredId ).Any() == false select post;
Ive used .Any() == false
to model the NOT EXISTS
statement in Transact-SQL.
source share