Using a List in a where clause in Entity Framework

∥☆過路亽.° 提交于 2019-12-23 21:22:16

问题


I am trying to retrieve document id's over a one-to-many table. I want to use a List in the where clause to find all id's that are connected with every element in the list.

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.


回答1:


If you want that all d.Tags should be in the included in the _tags list, you can try:

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:

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.




回答2:


Use a join:

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


来源:https://stackoverflow.com/questions/12034321/using-a-list-in-a-where-clause-in-entity-framework

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!