无法创建类型' Data.Item'的常量值。此上下文仅支持原始类型或枚举类型

时间:2016-06-29 22:53:39

标签: c# entity-framework linq

我使用下面提到的查询来展平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

1 个答案:

答案 0 :(得分:0)

我认为在您收到错误的2个虚线中,您正在尝试使用数据库查询(非物化)数据集加入本地数据集(项目)。

Items.Where(x => x.ItemId == orderItemDetail.ItemId).FirstOrDefault().ItemName,

EF或LINQ2SQL不允许这样做。

你需要

  • 实现两个数据集,然后在内存中本地运行谓词,或
  • 将两个数据集都设为IQueryable或
  • 使用PredicateBuilder from LinqKit之类的东西,使用本地数据集为IQueryable构建谓词。

我认为最简单的方法是不在查询中使用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;