简体   繁体   中英

Linq ToArray in ToArray error

I'm trying to make a array of my model (Basket) with Linq and in that model i have another array, so when i'm creating that model with link i got this error:

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

I don't get an error when i return just one Basket model (with only the array of BasketLines).

My code is:

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

Since GetList returns IQueryable the actual SQL query is not executed until the last ToArray is called. So what happens here is that LINQ to SQL tries to translate whole your query into the SQL. Of course there is no ToArray analogue in SQL, and the translation fails.

What you can do is to run the query before you start projection onto the model:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM