带有内部和外部联接的LINQ查询中的NotSupportedException

时间:2013-03-12 06:12:20

标签: c# linq entity-framework-4 linq-to-entities

我有几个内部和外部联接的以下查询。该查询创建一个新类,该类不是实体框架的一部分,而是一个保存报告数据的类:

from T6340 in PayOrders
from T6351 in POrderAccPDocLines.Where(x=> T6340.Id == x.PaymentDocId)
from T6321 in PaymentDocLines.Where(x=> T6351.PaymentDocId == x.PaymentDocId &&  
     T6351.Line ==  x.Line)
from T6125 in ItemBillPDocLines.Where(x =>T6321.PaymentDocId == x.PaymentDocId &&     
     T6321.Line == x.LineId).DefaultIfEmpty(null)
from T6126 in ItemBillStockPDocs.Where(x => T6125.BillId== x.ItemBillId && 
     T6125.PaymentDocId == x.PaymentDocId && T6125.LineId == x.PDocLineId &&  
     T6125.SplitNumber == x.PDocSplitNumber).DefaultIfEmpty(null)
from T6126_A in ItemBillStockPDocs.Where(x => T6125.BillId == x.ItemBillId && 
     T6125.PaymentDocId == x.PaymentDocId && T6125.LineId == x.PDocLineId && 
     T6125.SplitNumber == x.PDocSplitNumber).DefaultIfEmpty(null)
from T6201 in StockTransactions.Where(x => T6126.TransactionId == x.Id && 
     T6126.TransactionSubId == x.SubId).DefaultIfEmpty(null)
where T6125.BillId == billId && T6321.PaymentDoc.Canceled == 0
group new
{
 T6321,T6125,T6351,T6126_A,T6340,T6201
}
by new
{
 T6321_PaymentDocId = T6321.PaymentDocId,
 T6321.Line,
 T6321.CurrencyId,
 T6321.Amount,
 T6125,                                
 T6351.GLAccount,
 T6351.PaymentOrderAmount,
 T6321.PaymentDoc.ReferenceCode,
 T6126_Quantity = T6126_A.Quantity,
 T6126_A.TransactionId,
 T6126_A.TransactionSubId,
 T6351.PaymentOrderId,
 T6340.PayCurrency,
 T6126_A.TransactionSplitNumber
} into grouped
select new PaymentDataStore()
{
    ItemBillPaymentDocLine = grouped.Key.T6125,
    Currency = grouped.Key.CurrencyId,
    Amount = grouped.Key.Amount,
    PaymentDocumentId = grouped.Key.T6321_PaymentDocId,
    Approved = 0,
    Rate = 0,
    MaxExecutionDate = grouped.Max(x=>x.T6201.ExecutionDate),
    GLAccount = grouped.Key.GLAccount,
    PaymentOrderAmount = grouped.Key.PaymentOrderAmount,
    Constant1 = 0,
    ReferenceDocument = grouped.Key.ReferenceCode,
    Quantity = grouped.Key.T6126_Quantity,
    TransactionId = grouped.Key.TransactionId,
    TransactionSubId = grouped.Key.TransactionSubId,
    Constant2 = 0,
    PaymentOrder = grouped.Key.PaymentOrderId,
    PaymentCurrency = grouped.Key.PayCurrency,
    TransationSplitNumber = grouped.Key.TransactionSplitNumber
   }).ToList()

当我执行查询时,我遇到了异常:

NotSupportedException: Unable to create a constant value of type 
'T6351_POrdAccPDocLine'. Only primitive types ('such as Int32, String, and 
Guid') are supported in this context.

我试图找到异常的原因而没有运气。

为什么我会一直异常?

* 编辑:*

我将查询的第二行更改为:

from T6340 in PayOrders
join T6351 in POrderAccPDocLines on T6340.Id equals T6351.PaymentDocId ...

现在我在T6321实体上得到了同样的例外。 我想这是做的方法(转换语句使用连接)但我不知道如何使用外连接(查询中的第5-6行),因为我没有DefaultIfEmpty()的选项使用join时。

我正在敲打我的脑袋一段时间。你能救我吗?

非常感谢。

2 个答案:

答案 0 :(得分:0)

我认为问题在于:

by new
{
  ...
  T6125,
  ...
}

LINQ to Entities引擎需要将表达式转换为SQL语句,如果在表达式中包含整个对象(而不是基本类型),则无法执行此操作。

答案 1 :(得分:0)

这可以帮助你......

from po in payOrders
join pod in POrderAccPDocLines on po.Id equals pod.PaymentDocId
join pdl in PaymentDocLines on new { Id = pod.PaymentDocId, Line = pod.Line } equals  new { Id = pdl.PaymentDocId, Line = pdl.Line }
join ibdl in ItemBillPDocLines on new { Id = pdl.PaymentDocId, Line = pdl.Line } equals  new { Id = ibdl.PaymentDocId, Line = ibdl.Line } into ItemBill  //Outer Join
from ibdl in ItemBill.DefaultIfEmpty() //Outer Join
join.......