我使用下面提到的查询来展平ObservableCollection:
using(RateDifferenceDBEntities db = new RateDifferenceDBEntities())
{
Items = new ObservableCollection<Item>(db.Items);
Orders = new ObservableCollection<ClaimDTO>(from order in db.Orders
from orderItemDetail in order.OrderItemDetails
from dispatch in order.Dispatches
from dispatchItemDetail in dispatch.DispatchItemDetails
where ((orderItemDetail.ItemId == dispatchItemDetail.ItemId && dispatchItemDetail.Rate > orderItemDetail.Rate)
&& (dispatch.DateOfDispatch >= StartDate && dispatch.DateOfDispatch <= EndDate))
select new ClaimDTO
{
SalesOrderId = order.SalesOrderId,
DateOfOrder = order.DateOfOrder,
PartyName = order.Party.PartyName,
------------> OrderItemName = Items.Where(x => x.ItemId == orderItemDetail.ItemId).FirstOrDefault().ItemName,
OrderQuantity = orderItemDetail.Quantity,
OrderRate = orderItemDetail.Rate,
OrderAmount = orderItemDetail.Amount,
SalesInvoiceId = dispatch.SalesInvoiceId,
DateOfDispatch = dispatch.DateOfDispatch,
------------> DispatchItemName = Items.Where(x => x.ItemId == dispatchItemDetail.ItemId).FirstOrDefault().ItemName,
DispatchQuantity = dispatchItemDetail.Quantity,
DispatchRate = dispatchItemDetail.Rate,
DispatchAmount = dispatchItemDetail.Amount
});
return Orders;
}
你能看到上面代码中的两个虚线箭头吗?
我在那里发出错误:
无法创建类型&#39; Data.Item&#39;的常量值。在此上下文中仅支持原始类型或枚举类型。
以下是我用于构建上述查询的Heirarchical集合:
Order //Collection
|-OrderId
|-DateOfOrder
|-PartyName
|-OrderDetails //Collection
| |-ItemName
| |-Quantity
| |-Rate
| |-Amount
|-Dispatch //Collection
| |-InvoiceNo
| |-DateOfDispatch
| |-DispatchDetails //Collection
| | |-ItemName
| | |-Quantity
| | |-Rate
| | |-Amount
答案 0 :(得分:0)
我认为在您收到错误的2个虚线中,您正在尝试使用数据库查询(非物化)数据集加入本地数据集(项目)。
Items.Where(x => x.ItemId == orderItemDetail.ItemId).FirstOrDefault().ItemName,
EF或LINQ2SQL不允许这样做。
你需要
我认为最简单的方法是不在查询中使用Observale Items变量并使用db.Items。
Orders = new ObservableCollection<ClaimDTO>(from order in db.Orders
from goodItems in db.Items
from orderItemDetail in order.OrderItemDetails
from dispatch in order.Dispatches
from dispatchItemDetail in dispatch.DispatchItemDetails
where ((orderItemDetail.ItemId == dispatchItemDetail.ItemId && dispatchItemDetail.Rate > orderItemDetail.Rate)
&& (dispatch.DateOfDispatch >= StartDate && dispatch.DateOfDispatch <= EndDate))
select new ClaimDTO
{
SalesOrderId = order.SalesOrderId,
DateOfOrder = order.DateOfOrder,
PartyName = order.Party.PartyName,
------------> OrderItemName = goodItems.Where(x => x.ItemId == orderItemDetail.ItemId).FirstOrDefault().ItemName,
OrderQuantity = orderItemDetail.Quantity,
OrderRate = orderItemDetail.Rate,
OrderAmount = orderItemDetail.Amount,
SalesInvoiceId = dispatch.SalesInvoiceId,
DateOfDispatch = dispatch.DateOfDispatch,
------------> DispatchItemName = goodItems.Where(x => x.ItemId == dispatchItemDetail.ItemId).FirstOrDefault().ItemName,
DispatchQuantity = dispatchItemDetail.Quantity,
DispatchRate = dispatchItemDetail.Rate,
DispatchAmount = dispatchItemDetail.Amount
});
return Orders;