Double is not a NULL type

I am doing this with LINQ:

// let use Linq var DateMarket = from p in Orders_From_CRD.AsEnumerable() where p.Field<Double>("Fill_ID") != null select OrderTable.Rows.Add(p.Field<DateTime>("trade_date"), p.Field<string>("ticker"), p.Field<Double>("EXEC_QTY"), p.Field<Double>("EXEC_PRICE")); TradeTable = DateMarket.CopyToDataTable(); 

But I have a mistake telling me

 Cannot cast DBNull.Value to type 'System.Double'. Please use a nullable type. 

Do you know how to use the NULL type in this case?

I tried <Double?> And I got 'Specified cast is not valid.'

+7
source share
3 answers

You can use:

 p.Field<Double?> 

T? is a shorthand for Nullable<T> , a wrapper for a value type that allows null values.

+5
source

Use Type:

 Double? 

when you need double double

+4
source

by default, you cannot assign double values ​​to zero. The solution to this problem is to use a type with a null value: Double?

try the following:

  var DateMarket = from p in Orders_From_CRD.AsEnumerable() where p.Field<Double?>("Fill_ID") != null select OrderTable.Rows.Add(p.Field<DateTime>("trade_date"), p.Field<string>("ticker"), p.Field<Double?>("EXEC_QTY"), p.Field<Double?>("EXEC_PRICE")); TradeTable = DateMarket.CopyToDataTable(); 
+3
source

All Articles