Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Double is not a nullable type [closed]

Tags:

c#

linq

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 an error telling me

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

Do you know how to cast nullable type in this case ?

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

like image 395
francops henri Avatar asked Feb 17 '12 16:02

francops henri


People also ask

Can Double have null?

Java primitive types (such as int , double , or float ) cannot have null values, which you must consider in choosing your result expression and host expression types.

Can doubles be null C#?

In the C# programming language, all basic numeric data types (int, float, double, bool, and others) are value types. These types can never be set to null.

How do you handle double as null in C#?

Short answer: You can't. A double can only contain a valid double-precision floating point value.

How do you add a null to a double?

If you want to assign null value or no value for this double variable then you have to give value like 0.0.


3 Answers

You could use:

p.Field<Double?>

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

like image 78
Jackson Pope Avatar answered Oct 17 '22 23:10

Jackson Pope


Use the type :

Double?  

when you need a nullable double

like image 4
ThePaye Avatar answered Oct 18 '22 00:10

ThePaye


by default, double can't be assigned null values. The solution to this problem is to use a nullable type : Double?

try this:

        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();
like image 3
Akrem Avatar answered Oct 18 '22 01:10

Akrem