2016-04-22 6 views
0

私はlinqクエリを作成しているデータセットを持っています。Linqクエリでnullをチェックする

var results = dtWorkCenters.AsEnumerable() 
      .Where(x => x.Field<string>("Work_Center") == theWorkCenter && x.Field<string>("Job") == theJob) 
      .Select(x => new 
       { 
        Operation = x.Field<string>("Job_Operation"), 
        WorkDate = x.Field<DateTime>("Work_Date"), 
        Employee = x.Field<string>("Employee"), 
        SetupHours = x.Field<double>("Act_Setup_Hrs"), 
        SetupDollars = x.Field<double>("Act_Labor_Dollars"), 
        RunHours = x.Field<double>("Act_Run_Hrs"), 
        RunDollars = x.Field<double>("Act_Labor_Dollars") 
       }); 

フィールドJob_Operationはデータベースの文字列ですが、NULL値を含むことがあります。上記の構文は、NULL値で失敗します。データのNULLを守るにはどうすればよいですか?

+1

は、あなたの問題は、 'STRING'フィールドであることを確認しています? 'string'は' null'値を保持することができますが、 'DateTime'と' double'は保持できません。 –

+0

はい、クエリから文字列フィールドを削除でき、正常に動作します。 –

答えて

1

私はあなたがnull値を持っている問題は、nullに値型(DateTimedouble)の特性(WorkDateSetupHoursなど)を設定しようとした結果かもしれないと思います。

その場合は、そのようx.Field<T>にあなたの呼び出しでDateTime?double?を使用して、汎用Field型としてNullable<DateTime>Nullable<double>を指定してみてください:

var results = dtWorkCenters.AsEnumerable() 
        .Where(x => x.Field<string>("Work_Center") == theWorkCenter 
            && x.Field<string>("Job") == theJob) 
        .Select(x => new 
           { 
            Operation = x.Field<string>("Job_Operation"), 
            WorkDate = x.Field<DateTime?>("Work_Date"), 
            Employee = x.Field<string>("Employee"), 
            SetupHours = x.Field<double?>("Act_Setup_Hrs"), 
            SetupDollars = x.Field<double?>("Act_Labor_Dollars"), 
            RunHours = x.Field<double?>("Act_Run_Hrs"), 
            RunDollars = x.Field<double?>("Act_Labor_Dollars") 
           }); 
関連する問題