【问题标题】:LINQAnonymous class, property set to nullLINQAnonymous 类,属性设置为 null
【发布时间】:2011-03-08 08:34:24
【问题描述】:

我在从 LINQ 中的匿名类中的属性返回可为空的 double 和 int 时遇到问题。我的选择语句在这里:


return from workS in db.vWorkstations
         join custF1 in db.CustomFields.Where(x => x.CustomFieldID == cuField[0].ID) 
         on intAreaNumber equals custF1.Area into tmpCust1
       from custF1 in tmpCust1.DefaultIfEmpty()
         join custFV1 in db.CustomFieldValues 
         on new DummyClass{P1 = workS.WorkstationID, P2 = custF1.CustomFieldID} 
           equals new DummyClass
                  {
                     P1 = custFV1.WorkstationID.HasValue ? custFV1.WorkstationID.Value : -1 
                     , P2 = custFV1.CustomFieldID
                  } into tmpCustFV1
       from custFV1 in tmpCustFV1.DefaultIfEmpty()
       select new 
              { 
                 WorkstationID = workS.WorkstationID
                 , CallName = workS.CallName
                 , CustomFieldString1 = (cuField[0].TypeID == 1) ? custFV1.FieldValue : "" 
                 , CustomFieldSelection1 = (cuField[0].TypeID == 2) ? custFV1.FieldValue : "" 
                 , CustomFieldNumber1 = (custFV1.FieldValue != null && cuField[0].TypeID == 3) ? new Nullable(int.Parse(custFV1.FieldValue)) : (int?)null
                 , CustomFieldAmount1 = (custFV1.FieldValue != null && cuField[0].TypeID == 4) ? new Nullable(Double.Parse(custFV1.FieldValue)) : new Nullable()
                 , CustomFieldDateTime1 = (custFV1.FieldValue != null && cuField[0].TypeID == 5) ? new Nullable(DateTime.ParseExact(custFV1.FieldValue,"yyyyMMdd",provider)) : new Nullable() 
              };

它编译,但不运行。问题出在线条上

 ,CustomFieldNumber1 = (custFV1.FieldValue != null && cuField[0].TypeID == 3) 
     ? new Nullable<int>(int.Parse(custFV1.FieldValue))
     : (int?)null

 ,CustomFieldAmount1 = (custFV1.FieldValue != null && cuField[0].TypeID == 4) 
     ? new Nullable<Double>(Double.Parse(custFV1.FieldValue))
     : new Nullable<Double>()

如您所见,我尝试了两种返回空值的不同方法,但都不起作用。如果我删除解析,只返回一个字符串,它就可以工作。不幸的是,这不是我的选择。

查询失败并出现错误:

base {System.SystemException} = {"参数 'value' 的类型错误。应为 'System.Nullable1[System.Double]'. Actual 'System.Nullable1[System.Int32]'。"}

可能出了什么问题?提前致谢

【问题讨论】:

  • 异常是从Parse方法内部抛出的吗?
  • 不,来源是“System.Data.Linq”。而且我确信输入是可解析的..
  • 您发布的第二个代码块与第一个代码块有些不同:第一个代码块是 new Nullable(),第二个代码块是 new Nullable&lt;Double&gt;()。您能否更新查询,使其与您正在运行的代码相同?
  • WorkstationID 的类型是什么?如果是字符串,则需要将 -1 引用为 "-1"

标签: c# linq nullable anonymous-class


【解决方案1】:

你不应该在这里只返回 null -

 ,CustomFieldNumber1 = (custFV1.FieldValue != null && cuField[0].TypeID == 3) 
         ? new Nullable<int>(int.Parse(custFV1.FieldValue))
         : null

【讨论】:

    【解决方案2】:

    是的,我在 Nullable() 中看到了这一点。我最终做了一个解决方法。在数据库中创建了一个函数,如果被告知,它将以十进制或 null 形式返回原始值。有效,但可能不是那么漂亮。

    ,CustomFieldAmount1 =  db.fn_ConvertVarcharToDecimal(custFV1.FieldValue,(custFV1.FieldValue != null && cuField[0].TypeID != 4) ) 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-09-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-17
      • 1970-01-01
      • 2014-08-25
      相关资源
      最近更新 更多