C# 具有子项的Linq查询(实体框架)
调用rest/wcf服务时,我收到如下错误消息 LINQ to实体无法识别该方法 'System.Collections.Generic.List1[Receipt.Service.Entities.Item] ToList[Item]System.Collections.Generic.IEnumerable1[Receipt.Service.Entities.Item]' 方法,而此方法无法转换为存储表达式 我正在使用实体框架,这是代码 var响应=新的GetReceiptByIdResponseC# 具有子项的Linq查询(实体框架),c#,linq,entity-framework,entity-framework-4,entity-framework-5,C#,Linq,Entity Framework,Entity Framework 4,Entity Framework 5,调用rest/wcf服务时,我收到如下错误消息 LINQ to实体无法识别该方法 'System.Collections.Generic.List1[Receipt.Service.Entities.Item] ToList[Item]System.Collections.Generic.IEnumerable1[Receipt.Service.Entities.Item]' 方法,而此方法无法转换为存储表达式 我正在使用实体框架,这是代码 var响应=新的GetReceiptByIdRespo
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(),
这是我模特的照片。
项目是一个项目列表
我的问题是如何获取项目并为我的响应消息构造它?
提前谢谢你的帮助 我认为您不必逐个映射每个属性,EF将自动映射所有标量属性,因为您正在使用EF entity new Entities.Receive来构造结果,而不使用某种视图模型或DTO。您只需返回包含项目和存储的收据。收据::项目将仅返回引用收据的数据 这样做可以简化查询
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;
为什么要逐个映射每个属性的收据,为什么不先返回收据,然后创建新的响应并设置收据属性?你为什么要这样做,i.ReceiptUniqueId==ID?ReceiptUniqueId是Receipt和r的主键。Items将只包含属于ReceiptThank的Items感谢我的朋友,我可以完全使用您的代码,因为我要将实体模型转换为类响应。谢谢你所做的一切D乔治