在Entity Framework的where子句中使用List

我试图通过一对多表检索文档ID。 我想在where子句中使用List来查找与列表中的每个元素相关联的所有id。

List docIds = (from d in doc where _tags.Contains(d.Tags) select d.id).ToList(); 

我知道包含的内容必须不正确,但我无法解决。 如果我尝试foreach,我无法弄清楚如何检查文档是否包含所有标签。

如果您希望所有d.Tags都包含在_tags列表中,您可以尝试:

 List docIds = (from d in doc where d.Tags.All(t => _tags.Contains(t)) select d.id).ToList(); 

如果你想要那个d.Tags应该包含你需要的d.Tags中的所有项目:

 List docIds = (from d in doc where _tags.All(t => d.Tags.Contains(t)) select d.id).ToList(); 

但是我不知道它是如何通过EF转换为SQL的,所以也许你需要在客户端站点上对它进行评估。

使用联接:

 List docIds = (from d in doc from t in tags where d.Tags.Contains(t) select d.id).ToList();