如何做一个“in”查询在实体框架中? [英] How to do an "in" query in entity framework?

查看:88
本文介绍了如何做一个“in”查询在实体框架中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在列表中选择一个列表中的按键列表中的行?这样做:

How can I do a select in linq to entities to select rows with keys from a list? Something like this:

var orderKeys = new int[] { 1, 12, 306, 284, 50047};
var orders = (from order in context.Orders 
              where (order.Key in orderKeys) 
              select order).ToList();
Assert.AreEqual(orderKeys.Count, orders.Count);

我尝试使用一些答案中提到的包含方法,它不起作用并抛出这个异常:

I tried using the Contains method as mentioned in some of the answers but it does not work and throws this exception:

LINQ to Entities不识别方法'Boolean Contains [Int32](System.Collections.Generic.IEnumerable` 1 [System.Int32],Int32)'方法,此方法无法转换为存储表达式。

推荐答案

p> 尝试这样:

Try this:

var orderKeys = new int[] { 1, 12, 306, 284, 50047};
var orders = (from order in context.Orders 
              where orderKeys.Contains(order.Key);
              select order).ToList();
Assert.AreEqual(orderKeys.Count, orders.Count);

编辑:我找到了一些解决方法 - 请参阅 WHERE IN子句?

I have found some workarounds for this issue - please see WHERE IN clause?:


实体框架不
目前支持收集价值
参数('statusesToFind '在你的
示例中)。要解决这个
限制,您可以使用以下
实用程序方法手动
构造一个表达式给出一个
的值序列:

The Entity Framework does not currently support collection-valued parameters ('statusesToFind' in your example). To work around this restriction, you can manually construct an expression given a sequence of values using the following utility method:

这篇关于如何做一个“in”查询在实体框架中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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