如何做一个"在"查询实体框架? [英] How to do an "in" query in entity framework?
问题描述
我如何做一个选择在LINQ to实体选择从列表中键的行?事情是这样的:
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到实体不能识别方法布尔ContainsInt32的方法,而这种方法不能被翻译成店前pression。的
推荐答案
试试这个:
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);
修改我已经找到了一些解决方法针对此问题 - 请<一href="http://social.msdn.microsoft.com/forums/en-US/adodotnetentityframework/thread/095745fe-dcf0-4142-b684-b7e4a1ab59f0/">WHERE IN子句:
I have found some workarounds for this issue - please see WHERE IN clause?:
实体框架不 目前支持集合值 参数(在'statusesToFind'你 例)。要解决此 限制,您可以手动 构造赋予了一个前pression 使用下列值序列 实用方法:
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:
这篇关于如何做一个&QUOT;在&QUOT;查询实体框架?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!