LINQ多对多关系,如何编写正确的WHERE子句?

Jea*_*uis 8 c# linq entity-framework-4 dbcontext

我为表格使用多对多关系.

有一个查询:

var query = from post in context.Posts
        from tag in post.Tags where tag.TagId == 10
        select post;
Run Code Online (Sandbox Code Playgroud)

好的,它工作正常.我得到的帖子有id指定的标签.

我有一组标签ID.我希望收到包含我的收藏品中每个标签的帖子.

我尝试以下方式:

var tagIds = new int[]{1, 3, 7, 23, 56};

var query = from post in context.Posts
        from tag in post.Tags where tagIds.Contains( tag.TagId )
        select post;
Run Code Online (Sandbox Code Playgroud)

它不起作用.该查询返回具有任何一个指定标记的所有帖子.

我希望得到一个这样的子句,但动态地对集合中的任何标签数量:

post.Tags.Whare(x => x.TagId = 1 && x.TagId = 3 && x.TagId = 7 && ... )
Run Code Online (Sandbox Code Playgroud)

Dou*_*las 29

你不应该在外部查询中投射每个帖子的标签; 相反,您需要使用内部查询来执行外部过滤器的检查.(在SQL中,我们曾经称它为相关子查询.)

var query = 
    from post in context.Posts
    where post.Tags.All(tag => tagIds.Contains(tag.TagId))
    select post;
Run Code Online (Sandbox Code Playgroud)

替代语法:

var query = 
    context.Posts.Where(post =>
        post.Tags.All(tag => 
            tagIds.Contains(tag.TagId)));
Run Code Online (Sandbox Code Playgroud)

编辑:纠正每个Slauma的澄清.下面的版本返回的帖子至少包含tagIds集合中的所有标签.

var query = 
    from post in context.Posts
    where tagIds.All(requiredId => post.Tags.Any(tag => tag.TagId == requiredId))
    select post;
Run Code Online (Sandbox Code Playgroud)

替代语法:

var query = 
    context.Posts.Where(post => 
        tagIds.All(requiredId => 
            post.Tags.Any(tag =>
                tag.TagId == requiredId)));
Run Code Online (Sandbox Code Playgroud)

编辑2:每个Slauma修正如上.还包括下面充分利用查询语法的另一种选择:

// 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;
Run Code Online (Sandbox Code Playgroud)

我曾经习惯在Transact-SQL中.Any() == false模拟NOT EXISTS运算符.

  • +1是唯一一个只匹配**ALL**tagIds的人,而不仅仅是**ANY** (2认同)