简体   繁体   中英

Linq query with child items (Entity Framework)

I am receiving an error message as below when Calling a rest/wcf service

LINQ to Entities does not recognize the method 'System.Collections.Generic.List 1[Receipt.Service.Entities.Item] ToList[Item](System.Collections.Generic.IEnumerable 1[Receipt.Service.Entities.Item])' method, and this method cannot be translated into a store expression.

I'm using Entity Framework and this is the code

var response = new GetReceiptByIdResponse();

var result = from r in Data.Instance.Receipts.Include("Items")
                where (r.ReceiptUniqueId == ID)
                select new GetReceiptByIdResponse
                {
                    Receipt = new Entities.Receipt()
                    {
                        ReceiptUniqueId = r.ReceiptUniqueId,
                        ReceiptId = r.ReceiptId,
                        CashierId = r.CashierId,
                        CashierName = r.CashierName,
                        ReceiptDateTime = r.ReceiptDateTime,
                        POSPlace = r.POSPlace,
                        Total = r.Total,
                        TotalTax = r.TotalTax,
                        TotalNet = r.TotalNet,
                        TotalDiscount = r.TotalDiscount,
                        Rounding = r.Rounding,
                        ItemAmount = r.ItemAmount,
                        Currency = r.Currency,
                        CurrencyCode = r.CurrencyCode,
                        MembershipCardUsed = r.MembershipCardUsed,
                        MembershipCardId = r.MembershipCardId,
                        Revoked = r.Revoked,
                        CardCompany = r.CardCompany,
                        FreeText = r.FreeText,
                        Items = (from i in r.Items
                                where i.ReceiptUniqueId == ID
                                select new Entities.Item()
                                {
                                    ItemUniqueId = i.ItemUniqueId,
                                    ItemId = i.ItemId,
                                    Ean = i.Ean,
                                    Name = i.Name,
                                    CurrentPrice = i.CurrentPrice,
                                    RegularPrice = i.RegularPrice,
                                    Color = i.Color,
                                    ItemRowCount = i.ItemRowCount,
                                    ItemOrder = i.ItemOrder,
                                    TotalGrossAmount = i.TotalGrossAmount,
                                    TotalNetAmount = i.TotalNetAmount,
                                    Removed = i.Removed
                                }).ToList(),
                        Store = new Store()
                        {
                            StoreId = r.Store.StoreId,
                            Name = r.Store.Name,
                            CorporateId = r.Store.CorporateId,
                            Adress = r.Store.Adress,
                            PostalCode = r.Store.PostalCode,
                            Phone = r.Store.Phone,
                            Email = r.Store.Email
                        }
                    }
                };

return response = result.FirstOrDefault() as GetReceiptByIdResponse;

I know the error is here becuase if i remove this part of code below it works, but I am not receiving any items

Items = (from i in r.Items
        where i.ReceiptUniqueId == ID
        select new Entities.Item()
        {
            ItemUniqueId = i.ItemUniqueId,
            ItemId = i.ItemId,
            Ean = i.Ean,
            Name = i.Name,
            CurrentPrice = i.CurrentPrice,
            RegularPrice = i.RegularPrice,
            Color = i.Color,
            ItemRowCount = i.ItemRowCount,
            ItemOrder = i.ItemOrder,
            TotalGrossAmount = i.TotalGrossAmount,
            TotalNetAmount = i.TotalNetAmount,
            Removed = i.Removed
        }).ToList(),

This is an image of my model. The Items is a List of item

实体框架模型

My question how can Get Items and construct it for my response message? Thanks for the help in advance. :)

I think you don't have to map it per property one by one, EF will automatically map all scalar properties since you are using EF entity ( new Entities.Receipt() ) to construct the result (not using somekind of view model or DTO). You can just return Receipt that includes Items and Store . Receipt::Items will only return data that refer to Receipt .

Your query can be simplified by just doing this.

var result = from r in Data.Instance.Receipts.Include("Items").Include("Store")
    where r.ReceiptUniqueId == ID
    select r;

var response = new GetReceiptByIdResponse
{
    Receipt = result.FirstOrDefault()
};

return response;

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