locked
Linq expression to return decimal value types RRS feed

  • Question

  • User-305496339 posted

    Hi Friends !!! I have a column in my database that is of type decimal(10,2). I am trying to return the sum of those columns using this linq expression 

    var result = db.ItemsPurchaseds.Sum(x => x.Price);  Its returning an incorrect value.  Thanks !!!

    Monday, February 18, 2019 6:52 AM

Answers

  • User475983607 posted

    Hi Friends !!! I have a column in my database that is of type decimal(10,2). I am trying to return the sum of those columns using this linq expression 

    var result = db.ItemsPurchaseds.Sum(x => x.Price);  Its returning an incorrect value.  Thanks !!!

    There is not much we can do without sample data that reproduces the issue along with expected results and actual results.

    Have you tried reading the SQL docs to make sure you are using correct significant figures?

    https://docs.microsoft.com/en-us/sql/t-sql/data-types/decimal-and-numeric-transact-sql?view=sql-server-2017

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, February 18, 2019 12:55 PM
  • User842257015 posted

    Hi,

    That would be much appreciated if you share some sample data if you can and expected and actual value you got..

    Please use dbcontext Log while executing Linq to SQL call.

    dbContext.Database.Log = Console.Write;
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, February 18, 2019 3:59 PM
  • User-893317190 posted

    Hi rkrex,

    I have made a test and have no problem with sum.

    Below is my table.

    1	Basic	10.23
    2	Day All	123.23
    3	HRA	15.78
    4	PA	126.13
    5	ESI	NULL

    My model.

        [Table("First")]
        public partial class First
        {
            public int id { get; set; }
    
            [StringLength(50)]
            public string Info { get; set; }
    
            public decimal? amount { get; set; }
        }

    My test code.

            DbContext1 context1 = new DbContext1();
            protected void Page_Load(object sender, EventArgs e)
            {
                Response.Write(context1.Firsts.Sum(f => f.amount.HasValue ? f.amount.Value :0));
              
            }

    The result.

    Could you show us the code you have problem with?

    Best regards,

    Ackerly Xu

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, February 19, 2019 3:23 AM
  • User-893317190 posted

    Hi rkrex,

    decimal? means nullable type of decimal. Normally  variable of type decimal couldn't be set to null . But if you add ? , it could be set to null.

    This is to prevent there is null value in your database . If your  column in database doesn't have null value , you could omit and remove the ?.

    As to f.amount.HasValue ? f.amount.Value :0 , it is an expression, which means if  f.amount.HasValue returns true , the value is f.amount.Value or (which means f.amount.value is null) zero is set.

    For more information about nullable type , you could refer to https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/nullable-types/

    Best regards,

    Ackerly Xu

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, February 20, 2019 1:10 AM

All replies

  • User475983607 posted

    Hi Friends !!! I have a column in my database that is of type decimal(10,2). I am trying to return the sum of those columns using this linq expression 

    var result = db.ItemsPurchaseds.Sum(x => x.Price);  Its returning an incorrect value.  Thanks !!!

    There is not much we can do without sample data that reproduces the issue along with expected results and actual results.

    Have you tried reading the SQL docs to make sure you are using correct significant figures?

    https://docs.microsoft.com/en-us/sql/t-sql/data-types/decimal-and-numeric-transact-sql?view=sql-server-2017

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, February 18, 2019 12:55 PM
  • User842257015 posted

    Hi,

    That would be much appreciated if you share some sample data if you can and expected and actual value you got..

    Please use dbcontext Log while executing Linq to SQL call.

    dbContext.Database.Log = Console.Write;
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, February 18, 2019 3:59 PM
  • User-893317190 posted

    Hi rkrex,

    I have made a test and have no problem with sum.

    Below is my table.

    1	Basic	10.23
    2	Day All	123.23
    3	HRA	15.78
    4	PA	126.13
    5	ESI	NULL

    My model.

        [Table("First")]
        public partial class First
        {
            public int id { get; set; }
    
            [StringLength(50)]
            public string Info { get; set; }
    
            public decimal? amount { get; set; }
        }

    My test code.

            DbContext1 context1 = new DbContext1();
            protected void Page_Load(object sender, EventArgs e)
            {
                Response.Write(context1.Firsts.Sum(f => f.amount.HasValue ? f.amount.Value :0));
              
            }

    The result.

    Could you show us the code you have problem with?

    Best regards,

    Ackerly Xu

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, February 19, 2019 3:23 AM
  • User-305496339 posted

    I was missing the where method to only get records that had the id of the parent record. What do the question mark and :0 mean ? Thanks !

    Tuesday, February 19, 2019 6:18 PM
  • User-893317190 posted

    Hi rkrex,

    decimal? means nullable type of decimal. Normally  variable of type decimal couldn't be set to null . But if you add ? , it could be set to null.

    This is to prevent there is null value in your database . If your  column in database doesn't have null value , you could omit and remove the ?.

    As to f.amount.HasValue ? f.amount.Value :0 , it is an expression, which means if  f.amount.HasValue returns true , the value is f.amount.Value or (which means f.amount.value is null) zero is set.

    For more information about nullable type , you could refer to https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/nullable-types/

    Best regards,

    Ackerly Xu

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, February 20, 2019 1:10 AM