簡體   English   中英

Linq ToArray在ToArray中出錯

[英]Linq ToArray in ToArray error

我正在嘗試使用Linq制作我的模型(Basket)的數組,並且在該模型中我有另一個數組,所以當我用鏈接創建該模型時我得到了這個錯誤:

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

當我只返回一個Basket模型(只有BasketLines數組)時,我沒有收到錯誤。

我的代碼是:

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();

由於GetList返回IQueryable在調用最后一個ToArray之前不會執行實際的SQL查詢。 所以這里發生的是LINQ to SQL嘗試將整個查詢轉換為SQL。 當然,SQL中沒有ToArray模擬,並且轉換失敗。

您可以做的是開始投影到模型之前運行查詢:

basketManager.GetList(context).ToList().Select(...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM