join子句中某个表达式的类型不正确。调用“组加入”时类型推断失败

时间:2012-07-24 07:41:45

标签: linq c#-4.0

var val1 =
                (from main in
                     (from irac in objIRAC
                      join iip in objIIP on irac.InvoiceItemID equals iip.InvoiceItemID
                      join oritc in
                          (from ritc in objRITC
                           join iips in objIIP on ritc.InvoiceItemPremiumID equals iips.InvoiceItemPremiumID
                           where ritc.ReconID == ReconId 
                                 && ritc.BookOfBusinessInvoiceItemPremiumID == BookOfBusinessInvoiceItemPremiumID 
                                 && ritc.InvoiceID == InvoiceId 
                           group new { ritc, iips } by new { ritc.ReconID, iips.InvoiceItemID }
                               into ritcs
                               select new
                               {
                                   ritcs.Key.ReconID,
                                   ritcs.Key.InvoiceItemID
                               }) on ReconId equals oritc.ReconID into oritcs // ReconId
                      from oritc in oritcs.DefaultIfEmpty()
                      where iip.InvoiceItemPremiumID == BookOfBusinessInvoiceItemPremiumID 
                      select new
                      {
                          oritc.InvoiceItemID,
                          irac.CommissionOnTypeID,
                          irac.BOBFamilyID,
                          irac.RateTypeID,
                          irac.Rate,
                          irac.CommissionAdjustmentFlag
                      })
                 join sub in objIRAC // Here getting the error
                 on new { main.InvoiceItemID, main.CommissionOnTypeID, main.BOBFamilyID, main.RateTypeID, main.Rate, main.CommissionAdjustmentFlag }
                 equals new { sub.InvoiceItemID, sub.CommissionOnTypeID, sub.BOBFamilyID, sub.RateTypeID, sub.Rate, sub.CommissionAdjustmentFlag }
                 into subs
                 from osub in subs.DefaultIfEmpty()
                 select new { osub.InvoiceItemID });

2 个答案:

答案 0 :(得分:0)

检查

的类型
  

main.InvoiceItemID,main.CommissionOnTypeID,main.BOBFamilyID,main.RateTypeID,main.Rate,main.CommissionAdjustmentFlag

  

sub.InvoiceItemID,sub.CommissionOnTypeID,sub.BOBFamilyID,sub.RateTypeID,sub.Rate,sub.CommissionAdjustmentFlag

他们应该完全匹配。也就是说:如果一个属性是Nullable<int>且相应的属性为int,则它不匹配。由于除了InvoiceItemID之外的所有属性都来自同一个实体,因此InvoiceItemID可能是罪魁祸首。

答案 1 :(得分:0)

赔率是你的一个外键引用可以为空,你需要进行显式转换才能使连接(假设内部连接)起作用。

var result =
  from a in context.T1
  join b in context.T2
    on new { s.Field1, s.Field2 }
    equals new { 
      Field1 = (int)b.T3.Field1 /* Join via sub-table, explicit conversion from int? to int  */
      ,Field2 = (int)b.Field2 /* explicit conversion from int? to int  */
    }
  where 
    b.T3.Field1 != null /* Specify no nulls allowed for inner-join fields */
    && b.Field2 != null
    && b.Field3 == InputVar1 /* Input Search Variables */
    && b.Field4 == InputVar2
  select a;

上面的示例是从我正在使用的实际查询中重命名的。