在实体框架在where子句中使用列表 [英] Using a List in a where clause in Entity Framework

查看:173
本文介绍了在实体框架在where子句中使用列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图通过一到多个表检索文档的ID。我想用一个列表在where子句中找到所有的ID是与列表中的每一个元素连接

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>();



我知道必须包含不正确,但我不能工作了。如果我尝试一个foreach我无法工作,如何检查,如果文档中包含的所有标签。

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.

推荐答案

如果你想要的所有的 d.Tags 应该是包含在 _tags 列表,你可以尝试:

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>();

如果您希望 d.Tags 应包含所有项目从 _tags 您需要:

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>();



但我不知道它是如何通过翻译EF所以也许你需要在评估它的SQL客户现场。

But I don't know how it translates to SQL by EF so maybe you need to evaluate it on the client site.

这篇关于在实体框架在where子句中使用列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆