Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# ToArray错误中的Linq ToArray_C#_Linq_Asp.net Web Api - Fatal编程技术网

C# ToArray错误中的Linq ToArray

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

我试图用Linq为我的模型(篮子)创建一个数组,在该模型中我有另一个数组,所以当我用link创建该模型时,我得到了以下错误:

<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(...