简体   繁体   English

LINQ to Entities无法将方法Generic.List(int)识别为Generic.IEnumerable(int)方法

[英]LINQ to Entities does not recognize the method Generic.List(int) to Generic.IEnumerable(int) method

The database contains Orders. 数据库包含订单。 Orders can be contained within a group of Orders. 订单可以包含在一组订单中。 For every group of Orders it could contain 1 to many Orders. 对于每组订单,它可以包含1到多个订单。

However, Orders could have a NULL value assigned GroupOrderId as previous Orders did not have the grouping concept. 但是,Orders可以为NULLO值分配GroupOrderId,因为之前的Orders没有分组概念。 Only new Orders enforce the concept of being added to a group. 只有新订单强制执行添加到组的概念。

The class structure to be populated in order to perform actions on each Order is 要为每个订单执行操作而要填充的类结构是

public class OrdersGroup
{
    public int? GroupOrderId { get; set; }
    public List<int> OrderIds { get; set; }
}

The linq statement linq声明

var workPacketOrdersList = (from o in db.Orders
                                    where
                                        o.GroupOrderId >= groupOrderIdMin && o.GroupOrderId <= groupOrderIdMax &&
                                        o.IsDeleted == false
                                    orderby o.WorkPacketId ascending
                                    group o by o.WorkPacketId
                                    into grp
                                    select new OrdersGroup
                                               {
                                                   GroupOrderId = grp.Key,
                                                   OrderIds = grp.Select(g => g.OrderId).ToList()
                                               }).ToList();

Full exception 完全例外

LINQ to Entities does not recognize the method 'System.Collections.Generic.List`1[System.Int32] ToList[Int32](System.Collections.Generic.IEnumerable`1[System.Int32])' method, and this method cannot be translated into a store expression.

I see that the returned type of the linq query is a List<OrdersGroup> . 我看到linq查询的返回类型是List<OrdersGroup>

If the final .ToList() is omitted from the query than the return type becomes an IQueryable<OrdersGroup> 如果查询中省略了最终的.ToList(),则返回类型将变为IQueryable<OrdersGroup>

No matter what action is performed next the result is an exception that this method cannot be translated into a store expression. 无论接下来执行什么操作,结果都是此方法无法转换为商店表达式的异常。

I have tried to remove the specific select new OrdersGroup into a more generic select new and then perform actions on this result only to find the same store expression exception. 我试图将特定的select new OrdersGroup删除到更通用的select new ,然后对此结果执行操作,仅查找相同的商店表达式异常。

Can someone give some insight into where this linq is incorrect? 有人可以透露一下这个linq不正确的地方吗?

this is the part that's failing - grp.Select(g => g.OrderId).ToList() - you can't have a .ToList() in the select clause. 这是失败的部分 - grp.Select(g => g.OrderId).ToList() - 你不能在select子句中有一个.ToList()。 remove that and you should be fine. 删除它,你应该没事。

The problem is that LINQ to Entities is attempting to convert your query into SQL. 问题是LINQ to Entities正在尝试将您的查询转换为SQL。 It doesn't know how translate ToList into SQL, so that's the problem. 它不知道如何将ToList转换为SQL,这就是问题所在。 You need to remove the call to ToList from inside your query. 您需要从查询中删除对ToList的调用。

That is, 那是,

OrderIds = grp.Select(g => g.OrderId).ToList()

LINQ to Entities can not convert that to SQL. LINQ to Entities无法将其转换为SQL。 Remove the call 删除电话

OrderIds = grp.Select(g => g.OrderId)

and if you need OrderIds to be a List<int> , do the call to ToList after you execute the query. 如果需要OrderIdsList<int> ,请在执行查询后调用ToList

It's because you're trying to call ToList() in a part of the query that will become raw SQL and executed at the source (ie SQL Server, not the CLR). 这是因为您试图在查询的一部分中调用ToList() ,该查询将成为原始SQL并在源(即SQL Server,而不是CLR)上执行。 I don't know exactly what your data is so I can't necessarily make an accurate recommendation on how to fix it but I would try taking making the ToList() call after this query or just not making it all. 我不确切知道你的数据到底是什么,所以我不一定能就如何修复它做出准确的建议,但我会尝试在此查询之后进行ToList()调用,或者只是不做全部。 It's likely IEnumberable will offer whatever functionality you need which is what the Select will return if you remove the ToList() call. IEnumberable可能会提供您需要的任何功能,如果您删除ToList()调用, Select将返回该功能。

By the way since I wasn't explicit, I'm referring to the ToList() call inside the select -(second to last line) OrderIds = grp.Select(g => g.OrderId).ToList() the other one is fine. 顺便说一句,因为我没有明确,我指的是select中的ToList()调用 - (倒数第二行) OrderIds = grp.Select(g => g.OrderId).ToList()另一个很好。 It's executed on the results of the SQL query which is perfectly fine, you just can't make calls to C# specific methods within a query that will be executed by the SQL provider. 它是在SQL查询的结果上执行的,完全没问题,你只是不能在SQL提供程序执行的查询中调用C#特定的方法。

Your problem is that you select a list in your select statement. 您的问题是您在select语句中选择了一个列表。

select new OrdersGroup
{
   GroupOrderId = grp.Key,
   OrderIds = grp.Select(g => g.OrderId).ToList()
   /////////////////////////////////////^^^^^^^^^HERE
}

What you need to do is change OrderIds to an IEnumerable<int> , and then get rid of the ToList . 您需要做的是将OrderIds更改为IEnumerable<int> ,然后删除ToList

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何修复“ Generic.List的最佳重载方法匹配” <T> .AddRange(Generic.IEnumerable <T> )具有一些无效的参数。”? - How to fix “the best overloaded method match for Generic.List<T>.AddRange(Generic.IEnumerable<T>) has some Invalid arguments.”? LINQ to Entities无法识别方法&#39;System.Collections.Generic.IEnumerable`1 - LINQ to Entities does not recognize the method 'System.Collections.Generic.IEnumerable`1 转换Generic.List <int?> 到Generic.List <int> - Convert Generic.List<int?> to Generic.List<int> 在我面前回答吗? (Generic.List需要类型为Generic.IEnumerable Error的模型项) - Answer Right in Front of Me? (Generic.List requires a model item of type Generic.IEnumerable Error) 如何从任务转换<generic.list<t> &gt; 到一个 Generic.IEnumerable<t> ? </t></generic.list<t> - How can I convert from a Task<Generic.List<T>> to a Generic.IEnumerable<T>? DefaultIfEmpty()引起“ System.NotSupportedException:LINQ to Entities无法识别方法&#39;System.Collections.Generic.IEnumerable&#39;” - DefaultIfEmpty() Causing “System.NotSupportedException: LINQ to Entities does not recognize the method 'System.Collections.Generic.IEnumerable'” LINQ to Entities 无法识别方法“System.Collections.Generic.Dictionary”2[System.Int32,System.String] ToDictionary - LINQ to Entities does not recognize the method 'System.Collections.Generic.Dictionary`2[System.Int32,System.String] ToDictionary LINQ查询在通用存储库中,LINQ to Entities无法识别该方法 - LINQ queries in a generic repository, LINQ to Entities does not recognize the method LINQ to Entities无法识别方法&#39;System.Collections.Generic.Dictionary` - LINQ to Entities does not recognize the method 'System.Collections.Generic.Dictionary` 如何修复“无法隐式转换Generic.IEnumerable类型的错误消息” <type> &#39;至&#39;Generic.List <type> ” - How do I fix an error message of “Cannot implicitly convert type Generic.IEnumerable<type>' to 'Generic.List<type>”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM