如何做一个"在"查询实体框架?实体、框架、如何做一个、QUOT

2023-09-02 01:46:01 作者:陌染

我如何做一个选择在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);

修改我已经找到了一些解决方法针对此问题 - 请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: