.Where(n => n.Tags.All(t => tags.Contains(t.DisplayName)))
这是目前构建的方式,你只会与Node
S其中在Node.Tags
每标签在tags
白名单,其中包括Node
s的有一个名字落得没有标签。
你可能想在子集使用回答here:
_DbContext.Nodes
.Where(n => !tags.Except(n.Tags.Select(t => t.DisplayName)).Any())
.Select(...
set1.Except(set2)
包含set1
元素不在set2
!set1.Except(set2).Any() == true
如果set2
包括set1
每一个元素
编辑
有人指出,在评论中,使用除了可能产生问题的查询,所以我想另一种选择是从数据库中获取的超集,并进一步筛选内的对象应用:
_DbContext.Nodes
// filter nodes with any of the input tags
.Where(n => n.Tags.Any(t => tags.Contains(t.DisplayName)))
// select the information required
.Select(n => new {
n.NodeNativeId,
n.NodeName,
ClassName = n.NodeClass.ClassName,
TagNames = n.Tags.Select(t => t.DisplayName) })
// materialize super set with database
.ToList()
// filter so that only nodes with all tags remain
.Where(n => !tags.Except(n.TagNames).Any())
// produce result in anonymous class
.Select(n => new { n.NodeNativeId, n.NodeName, n.ClassName })
.ToList();
编辑2
我只看到了一个又一个here可能为你工作,但它要求Tag.DisplayName
是独特,因为如果你有相同的DisplayName
多个标签也可能失败:
_dbContext.Nodes
.Where(n => n.Tags.Count(t => tags.Contains(t.DisplayName)) == tags.Count)
.Select(...
来源
2015-06-19 22:39:03
jjj
你必须添加'n.Tags.Any()&& n.Tags.All(...'。这消除了没有标签的节点。如果'tags'变量中存在真正的标记,我不会看到查询如何返回任何节点。 –