none
How To Convert A Nullable Type? RRS feed

  • Question

  • I wrote the function below to retrieve the total sales for a store and date.  It gave me a compilation error that it couldn't convert d_tot_net to decimal.  That field is a nullable decimal? type.  Is this the best way to convert the nullable field or what is the best way?

    private decimal GetNetSales(int iStoreNo, DateTime dBusDate)
    {
        decimal nTotal = 0;
        using (var oContext = new MyEntities())
        {
            nTotal = oContext.table
                    .Where(o => o.storeno == iStoreNo && o.busdate == dBusDate)
                    .Sum(o => -(decimal)o.d_tot_net);
        }
        return nTotal;
    }


    MCSD .NET developer in Dallas, Texas


    • Edited by DallasSteve Thursday, March 26, 2015 8:20 PM
    Thursday, March 26, 2015 8:19 PM

Answers

  • The decimal? type has a Value property which retrieves the decimal value if there is one:

                decimal d;
                decimal? sum = new Nullable<decimal>(); //=null
                if (sum.HasValue)
                    d = sum.Value;

    You cannot cast a decimal? without a value to a decimal like this so your code will fail if there are any objects without a d_tot_net value:

                decimal? sum = new Nullable<decimal>(); //=null
                decimal d = (decimal)sum; // ERROR!

    You probably only want to include the objects which actually have a value in the sum:

                using (var oContext = new MyEntities())
                {
                    nTotal = oContext.table
                            .Where(o => o.storeno == iStoreNo && o.busdate == dBusDate && o.d_tot_net.HasValue)
                            .Sum(o => -o.d_tot_net.Value);
                }

    Hope that helps. 


    Please remember to mark all helpful posts as answer to close the thread and then please start a new thread if you have a new question.

    Friday, March 27, 2015 1:14 PM

All replies

  • Hello DallasSteve,

    >>It gave me a compilation error that it couldn't convert d_tot_net to decimal.  That field is a nullable decimal? type.

    Could you please share the detail exception message with us and the related table and its entity class? According to your description, I created a similar test demo, however, it could work fine:

    using (DFDBEntities db=new DFDBEntities())
    
                {
    
                    var result = db.Tables.Sum(t => (decimal)t.Data);
    
                }

    The entity:

    public partial class Table
    
        {
    
            public int ID { get; set; }
    
            public Nullable<decimal> Data { get; set; }
    
    }

    >>Is this the best way to convert the nullable field or what is the best way?

    Since I could not reproduce this issue, my suggestion is that you could change the d_tot_net column to be non-nullable so that you do not need to cast the type anymore.

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Friday, March 27, 2015 6:58 AM
    Moderator
  • Fred

    The code that I posted does not produce the error.  It produced the error before I added (double).  My question is, Is that the best way and place in the code to convert the data type?  Your suggestion to make the field non-nullable is not a good solution because we want the field to be nullable.


    MCSD .NET developer in Dallas, Texas


    • Edited by DallasSteve Friday, March 27, 2015 12:40 PM
    Friday, March 27, 2015 12:40 PM
  • The decimal? type has a Value property which retrieves the decimal value if there is one:

                decimal d;
                decimal? sum = new Nullable<decimal>(); //=null
                if (sum.HasValue)
                    d = sum.Value;

    You cannot cast a decimal? without a value to a decimal like this so your code will fail if there are any objects without a d_tot_net value:

                decimal? sum = new Nullable<decimal>(); //=null
                decimal d = (decimal)sum; // ERROR!

    You probably only want to include the objects which actually have a value in the sum:

                using (var oContext = new MyEntities())
                {
                    nTotal = oContext.table
                            .Where(o => o.storeno == iStoreNo && o.busdate == dBusDate && o.d_tot_net.HasValue)
                            .Sum(o => -o.d_tot_net.Value);
                }

    Hope that helps. 


    Please remember to mark all helpful posts as answer to close the thread and then please start a new thread if you have a new question.

    Friday, March 27, 2015 1:14 PM
  • You would have to get a null value check in the Where clause

                    nTotal = oContext.table
                   
    .Where(o => o.storeno == iStoreNo && o.busdate == dBusDate && (decimal)o.d_tot_net.HasValue)
                   
    .Sum(o => -(decimal)o.d_tot_net);

    Friday, March 27, 2015 1:46 PM