C# 具有内部和外部联接的LINQ查询中的NotSupportedException

C# 具有内部和外部联接的LINQ查询中的NotSupportedException,c#,linq,entity-framework-4,linq-to-entities,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 && T6

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

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()选项

我要敲我的头一会儿。你能帮我吗


非常感谢。

我认为问题在于:

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

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

这可能有助于您

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.......

不走运。我从group by和select语句中删除了这些行,但仍然存在相同的异常。也许这一行无法解析:
MaxExecutionDate=grouped.Max(x=>x.T6201.ExecutionDate),
因为它位于
PaymentDataStore
的构造函数中,可能是一个模型。No,我对该行进行了注释,但仍然是相同的异常。该异常很可能是由于您在实体级别上的group by
group new{T6321、T6125、T6351、T6126_a、T6340、T6201}
尝试按基本类型分组(如果是选项)。此外,生成的sql可能是巨大的,因为它将按GROUPBY中使用的每个实体的每个字段分组。看看sql是如何产生的也许是个好主意。请仅提供将产生异常的查询的最小值以及其中使用的类/实体。