C# ToArray错误中的Linq ToArray
我试图用Linq为我的模型(篮子)创建一个数组,在该模型中我有另一个数组,所以当我用link创建该模型时,我得到了以下错误:C# ToArray错误中的Linq ToArray,c#,linq,asp.net-web-api,C#,Linq,Asp.net Web Api,我试图用Linq为我的模型(篮子)创建一个数组,在该模型中我有另一个数组,所以当我用link创建该模型时,我得到了以下错误: <Error> <Message>An error has occurred.</Message> <ExceptionMessage> LINQ to Entities does not recognize the method 'HTTP.Webshop.API.WebAPI.Models.BasketLine[] To
<Error>
<Message>An error has occurred.</Message>
<ExceptionMessage>
LINQ to Entities does not recognize the method 'HTTP.Webshop.API.WebAPI.Models.BasketLine[] ToArray[BasketLine](System.Collections.Generic.IEnumerable`1[HTTP.Webshop.API.WebAPI.Models.BasketLine])' method, and this method cannot be translated into a store expression.
</ExceptionMessage>
<ExceptionType>System.NotSupportedException</ExceptionType>
<StackTrace>
at System.Web.Http.ApiController.<InvokeActionWithExceptionFilters>d__1.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Web.Http.Dispatcher.HttpControllerDispatcher.<SendAsync>d__0.MoveNext()
</StackTrace>
</Error>
由于
GetList
返回IQueryable
,因此在调用最后一个ToArray
之前,不会执行实际的SQL查询。所以这里发生的是LINQtoSQL尝试将整个查询转换为SQL。当然,SQL中没有类似的ToArray
,转换失败
您可以做的是在开始投影到模型之前运行查询:
basketManager.GetList(context).ToList().Select(...
GetList
是否返回IQueryable
?嘿,安德烈,是的,GetList返回IQueryable。BasketLines返回一个ICollection。
basketManager.GetList(context).ToList().Select(...