【问题标题】:NotSupportedException in LINQ query with inner and outer joins带有内部和外部联接的 LINQ 查询中的 NotSupportedException
【发布时间】:2013-03-12 06:12:19
【问题描述】:

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

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

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

非常感谢。

【问题讨论】:

  • 这行可能无法解析:MaxExecutionDate = grouped.Max(x=>x.T6201.ExecutionDate),,因为它在 PaymentDataStore 的构造函数中,可能是一个模型。
  • 不,我评论了这一行,但仍然是同样的异常。
  • 异常很可能是您在实体级别上分组的结果group new { T6321,T6125,T6351,T6126_A,T6340,T6201 } 如果可以的话,请尝试按原始类型分组。此外,生成的 sql 可能是巨大的,因为它将按 group by 中使用的每个实体的每个字段进行分组。也许LinqPad 是查看生成了什么sql 的好主意。请仅提供会产生异常的最少查询以及其中使用的类/实体。

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


【解决方案1】:

我认为问题出在这里:

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

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

【讨论】:

  • 运气不好。我从 group by 和 select 语句中删除了行,但仍然是相同的异常。
【解决方案2】:

希望这可以帮助你......

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-09-14
    • 1970-01-01
    • 2015-01-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-25
    • 1970-01-01
    相关资源
    最近更新 更多