0

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

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() 选项使用连接时。

我正在敲我的头一会儿。你能帮助我吗?

非常感谢。

4

2 回答 2

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.......
于 2013-03-12T13:35:04.737 回答
0

我认为问题出在这里:

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

LINQ to Entities 引擎需要将您的表达式转换为 SQL 语句,如果您在表达式中包含整个对象(而不是原始类型),它就无法做到这一点。

于 2013-03-12T06:54:57.783 回答