带子项的Linq查询(实体框架)

时间:2014-09-14 07:11:18

标签: c# linq entity-framework entity-framework-4 entity-framework-5

在调用rest / wcf服务时,我收到如下错误消息

  

LINQ to Entities无法识别该方法   'System.Collections.Generic.List 1[Receipt.Service.Entities.Item] ToList[Item](System.Collections.Generic.IEnumerable 1 [Receipt.Service.Entities.Item])'   方法,并且此方法无法转换为商店表达式。

我正在使用Entity Framework,这是代码

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;

我知道这里的错误是因为如果我删除它下面的代码部分可行,但我没有收到任何项目

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

这是我模特的形象。 是项目列表

Entity Framework model

我的问题如何获取项目并为我的回复消息构建它? 我在这里先向您的帮助表示感谢。 :)

1 个答案:

答案 0 :(得分:0)

我认为你不必逐个映射每个属性,EF将自动映射所有标量属性,因为你使用EF实体(new Entities.Receipt())来构造结果(不使用某些属性)查看模型或DTO)。您只需返回包含ReceiptItems的{​​{1}}即可。 Store只会返回引用Receipt::Items

的数据

只需执行此操作即可简化您的查询。

Receipt