简体   繁体   English

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

[英]LINQ many-to-many relationship, how to write a correct WHERE clause?

I use many-to-many relationship for my tables. 我为表格使用多对多关系。

There is a query: 有一个查询:

var query = from post in context.Posts
        from tag in post.Tags where tag.TagId == 10
        select post;

Ok, it works fine. 好的,它工作正常。 I get posts having the tag specified by id. 我得到的帖子有id指定的标签。

I have a collection of tag ids. 我有一组标签ID。 And i want to get posts having every tag in my collection. 我希望收到包含我的收藏品中每个标签的帖子。

I try the following way: 我尝试以下方式:

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;

It doesn't work. 它不起作用。 The query returns all posts having ANY one of the specified tags. 该查询返回具有任何一个指定标记的所有帖子。

I want to get a clause like this but dynamicaly for any count of tags in the collection: 我希望得到一个这样的子句,但动态地对集合中的任何标签数量:

post.Tags.Whare(x => x.TagId = 1 && x.TagId = 3 && x.TagId = 7 && ... )

You shouldn't project each post's tags in the outer query; 你不应该在外部查询中投射每个帖子的标签; rather, you need to use an inner query which performs the check for the outer filter. 相反,您需要使用内部查询来执行外部过滤器的检查。 (In SQL, we used to call it a correlated subquery .) (在SQL中,我们曾经称它为相关子查询 。)

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

Alternate syntax: 替代语法:

var query = 
    context.Posts.Where(post =>
        post.Tags.All(tag => 
            tagIds.Contains(tag.TagId)));

Edit : Correcting per Slauma's clarification . 编辑 :纠正每个Slauma的澄清 The version below returns posts which contain, at least, all the tags in the tagIds collection. 下面的版本返回的帖子至少包含tagIds集合中的所有标签。

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

Alternate syntax: 替代语法:

var query = 
    context.Posts.Where(post => 
        tagIds.All(requiredId => 
            post.Tags.Any(tag =>
                tag.TagId == requiredId)));

Edit 2 : Corrected above per Slauma. 编辑2 :每个Slauma修正如上。 Also including another alternative making full use of 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;

I've used .Any() == false to simulate the NOT EXISTS operator in Transact-SQL. 我使用.Any() == false来模拟Transact-SQL中的NOT EXISTS运算符。

这实际上很容易做到:

var tags = context.Posts.Where(post => post.Tags.All(tag => tagIds.Contains(tag)));

Another option is to intersect the two lists if you want the collection of tags to ONLY contain the set you specify and no others: 如果您希望标记集合仅包含您指定的集合而不包含其他集合,则另一个选项是交叉两个列表:

var query = from post in context.Posts
  let tags = post.Tags.Select(x => x.Id).ToList()
  where tags.Intersect(tagIds).Count() == tags.Length
  select post;

Try it with Any . 尝试使用Any

var query = from post in context.Posts
    from tag in post.Tags where tagIds.Any(t => t == tag.TagId )
    select post;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM