[英]LINQ many-to-many relationship, how to write a correct WHERE clause?
我为表格使用多对多关系。
有一个查询:
var query = from post in context.Posts
from tag in post.Tags where tag.TagId == 10
select post;
好的,它工作正常。 我得到的帖子有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;
它不起作用。 该查询返回具有任何一个指定标记的所有帖子。
我希望得到一个这样的子句,但动态地对集合中的任何标签数量:
post.Tags.Whare(x => x.TagId = 1 && x.TagId = 3 && x.TagId = 7 && ... )
你不应该在外部查询中投射每个帖子的标签; 相反,您需要使用内部查询来执行外部过滤器的检查。 (在SQL中,我们曾经称它为相关子查询 。)
var query =
from post in context.Posts
where post.Tags.All(tag => tagIds.Contains(tag.TagId))
select post;
替代语法:
var query =
context.Posts.Where(post =>
post.Tags.All(tag =>
tagIds.Contains(tag.TagId)));
编辑 :纠正每个Slauma的澄清 。 下面的版本返回的帖子至少包含tagIds
集合中的所有标签。
var query =
from post in context.Posts
where tagIds.All(requiredId => post.Tags.Any(tag => tag.TagId == requiredId))
select post;
替代语法:
var query =
context.Posts.Where(post =>
tagIds.All(requiredId =>
post.Tags.Any(tag =>
tag.TagId == requiredId)));
编辑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;
我使用.Any() == false
来模拟Transact-SQL中的NOT EXISTS
运算符。
这实际上很容易做到:
var tags = context.Posts.Where(post => post.Tags.All(tag => tagIds.Contains(tag)));
如果您希望标记集合仅包含您指定的集合而不包含其他集合,则另一个选项是交叉两个列表:
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;
尝试使用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.