Reputation: 414
I have a dataset on which I'm making a linq query.
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")
});
The field Job_Operation is a string int the database, but may contain a null value. The above syntax fails on the null values. How do I protect against nulls in the data?
Upvotes: 0
Views: 264
Reputation: 2226
I think the issue you are having with null
values may be a result of trying to set the value type (DateTime
and double
) properties (WorkDate
, SetupHours
, etc.) to null
.
If that is the case, try specifying Nullable<DateTime>
and Nullable<double>
as the generic Field
type by using DateTime?
and double?
in your calls to x.Field<T>
like so:
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")
});
Upvotes: 1