[英]Using a List in a where clause in Entity Framework
I am trying to retrieve document id's over a one-to-many table. 我试图通过一对多表检索文档ID。 I want to use a List in the where clause to find all id's that are connected with every element in the list.
我想在where子句中使用List来查找与列表中的每个元素相关联的所有id。
List<int> docIds = (from d in doc
where _tags.Contains(d.Tags)
select d.id).ToList<int>();
I know that the contains must be incorrect but I can't work it out. 我知道包含的内容必须不正确,但我无法解决。 If I try a foreach I can't work out how to check if the document contains all Tags.
如果我尝试foreach,我无法弄清楚如何检查文档是否包含所有标签。
If you want that all d.Tags
should be in the included in the _tags
list, you can try: 如果您希望所有
d.Tags
都包含在_tags
列表中,您可以尝试:
List<int> docIds = (from d in doc
where d.Tags.All(t => _tags.Contains(t))
select d.id).ToList<int>();
If you want that d.Tags
should contain all the item from _tags
you need: 如果您希望
d.Tags
应包含所有项目_tags
您需要:
List<int> docIds = (from d in doc
where _tags.All(t => d.Tags.Contains(t))
select d.id).ToList<int>();
But I don't know how it translates to SQL by EF so maybe you need to evaluate it on the client site. 但是我不知道它是如何通过EF转换为SQL的,所以也许你需要在客户端站点上对它进行评估。
Use a join: 使用联接:
List<int> docIds = (from d in doc
from t in tags
where d.Tags.Contains(t)
select d.id).ToList<int>();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.