首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Linq ToArray在ToArray误差中的应用

Linq ToArray在ToArray误差中的应用
EN

Stack Overflow用户
提问于 2014-03-06 14:35:19
回答 1查看 726关注 0票数 1

我试着用Linq制作我的模型(篮)的数组,在这个模型中我有另一个数组,所以当我用链接创建模型时,我得到了以下错误:

代码语言:javascript
复制
<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>

当我只返回一个篮模型(只包含BasketLines数组)时,不会出现错误。

我的代码是:

代码语言:javascript
复制
return basketManager.GetList(context).Select(b => new Models.Basket
                {
                    BasketId = b.BasketId,
                    CustomerId = b.CustomerId,
                    UserId = b.UserId,
                    ASPNETSessionId = b.ASPNETSessionId,
                    Created = b.Created,
                    CreatedOrder = b.CreatedOrder,
                    ShipmentMethodId = b.ShipmentMethodId,
                    ShipmentMethodName = (b.ShipmentMethod != null) ? b.ShipmentMethod.Description : null,
                    ShippingDocument = b.ShippingDocument,
                    VoucherCode = b.VoucherCode,
                    LockedSince = b.LockedSince,
                    PickupLocationId = b.PickupLocationId,
                    Reference = b.Reference,
                    Comments = b.Comments,
                    PurchaseNumber = b.PurchaseNumber,
                    DesiredDeliveryDate = b.DesiredDeliveryDate,
                    CompanyNameDelivery = b.CompanyNameDelivery,
                    NameDelivery = b.NameDelivery,
                    AddressDelivery = b.AddressDelivery,
                    Address2Delivery = b.Address2Delivery,
                    PostalCodeDeliver = b.PostalCodeDelivery,
                    CityDelivery = b.CityDelivery,
                    CountryISOCodeDelivery = b.CountryISOCodeDelivery,
                    InvoiceDiscount = b.InvoiceDiscount,
                    Modified = b.Modified,
                    BasketLines = b.BasketLines.Select(bl => new Models.BasketLine
                    {
                        BasketLineId = bl.BasketLineId,
                        BasketId = bl.BasketId,
                        ProductId = bl.ProductId,
                        ProductName = bl.Product.Name,
                        ProductVariantId = bl.ProductVariantId,
                        ProductVariantName = bl.ProductVariant.Name,
                        Quantity = bl.Quantity,
                        QuantityByPiece = bl.QuantityByPiece,
                        VATPercentage = bl.VATPercentage,
                        Amount = bl.Amount,
                        CalculatorAmount = bl.CalculatorAmount,
                        Discount = bl.Discount,
                        Reference = bl.Reference,
                        Comments = bl.Comments,
                        LockedSince = bl.LockedSince,
                        StockNr = bl.StockNr,
                        Created = bl.Created,
                        Modified = bl.Modified
                    }).ToArray()
                }).ToArray();
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-03-06 14:48:28

因为GetList返回IQueryable,所以在调用最后一个ToArray之前不会执行实际的ToArray查询。因此,这里发生的是LINQ试图将整个查询转换为SQL。当然,在SQL中没有ToArray模拟,并且转换失败。

您可以做的是在开始投影到模型之前运行查询:

代码语言:javascript
复制
basketManager.GetList(context).ToList().Select(...
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22227538

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档