0

I am using Linq and keep getting a DBNull error since my datatable contains DBNulls. I have:

var Sum = (from dt in dt1.AsEnumerable()
                 where (dtCutOffDate.Subtract(dt.Field<DateTime>("Date")).Days) >= iStartRange
                 && dtCutOffDate.Subtract(dt.Field<DateTime>("Date")).Days <= iEndRange
                 && ((items == null) || (dt.Field<string>("AcctNo") == items))
                 && (dt.Field<string>("type").Contains("6") || dt.Field<string>("type").Contains("9"))
                 select dt.Field<double>("amount")).Sum();

The date column is the one that is throwing the dbnull error. Is there any way to check for dbnull? If I use where date != dbnull.value, c# will not accept the code. I have tried <datetime?> but c# still doesn't like the code. Would anyone happen to know of a solution?

2
Contributors
2
Replies
11
Views
2 Years
Discussion Span
Last Post by lithium112
1

Instead of

 where (dtCutOffDate.Subtract(dt.Field<DateTime>("Date")).Days) >= iStartRange
                 && dtCutOffDate.Subtract(dt.Field<DateTime>("Date")).Days <= iEndRange

Just add a '?' character.

 where (dtCutOffDate.Subtract(dt.Field<DateTime?>("Date")).Days) >= iStartRange
                 && dtCutOffDate.Subtract(dt.Field<DateTime?>("Date")).Days <= iEndRange

You should avoid the exception now.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.