0

LINQの匿名クラスのプロパティからnull許容のdoubleとintを返すことに問題があります。私のselectステートメントはここにあります:


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

ご覧のとおり、null値を返す2つの異なるアプローチを試しましたが、いずれも機能しません。解析を削除して文字列を返すだけで、機能します。残念ながら、これは私にとっての選択肢ではありません。

クエリは次のエラーで失敗します。

base {System.SystemException}={"引数'value'は間違った型でした。予期された'System.Nullable1 1[System.Double]'. Actual 'System.Nullable[System.Int32]'。"}

何が間違っている可能性がありますか?前もって感謝します

4

2 に答える 2

0

ええ、私は Nullable() でそれを見ました。私は最終的に回避策を作りました。指示された場合、元の値を 10 進数または null として返すデータベース内の関数を作成しました。動作しますが、それほどきれいではないかもしれません。

,CustomFieldAmount1 =  db.fn_ConvertVarcharToDecimal(custFV1.FieldValue,(custFV1.FieldValue != null && cuField[0].TypeID != 4) ) 
于 2011-03-09T07:15:21.227 に答える