locked
Linq - rounding up the average? RRS feed

  • Question

  • User-745958333 posted

    Hi.

    If I have the following nested query:

    AverageRating = (from pqr in db.PQuote where pqr.PQuoteRating != null select pqr.PQuoteRating).Average()

    How can I round that average up to the nearest whole number?  I've tried this but it doesn't like it:

    AverageRating = Math.Round((from pqr in db.PQuote where pqr.PQuoteRating != null select pqr.PQuoteRating).Average())

    Thanks...

    Monday, February 1, 2016 11:18 AM

Answers

  • User-271186128 posted

    Hi Captain Planet,

    When we use Average() method, it will return a decimal? type value. So, before using the Math.Round method, you could use Convert.ToDecimal() method to convert the average value. Please refer to the following code:

    AverageRating = Math.Round(Convert.ToDecimal((from pqr in db.PQuote where pqr.PQuoteRating != null select pqr.PQuoteRating).Average()));

    Best regards,
    Dillion

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, February 2, 2016 4:20 AM
  • User-271186128 posted

    Hi Captain Planet,

    Additional information: LINQ to Entities does not recognize the method 'System.Decimal ToDecimal(System.Object)' method, and this method cannot be translated into a store expression.

    I suggest you could post the whole code about this LINQ query statement. In my previous reply the ToDecimal is outside the LINQ statement. Code like this:

    Var AverageRating = Math.Round(Convert.ToDecimal((from pqr in db.PQuote where pqr.PQuoteRating != null select pqr.PQuoteRating).Average()));
    
    

    The boldfaced words are the LINQ statement.

    Best regards,
    Dillion

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, February 5, 2016 9:16 AM

All replies

  • User1577371250 posted

    Hi,

    try this

    var query = from p in db.Table1
                let AverageRating = (from pqr in db.PQuote where pqr.PQuoteRating != null select pqr.PQuoteRating).Average()
                select new { 
    Name = p.Name,
    AverageRate = Math.Round(AverageRating)
    };

    Monday, February 1, 2016 11:29 AM
  • User-745958333 posted

    ....this looks good thanks.  I'll get back to you once I've tested....

    Monday, February 1, 2016 5:22 PM
  • User-271186128 posted

    Hi Captain Planet,

    When we use Average() method, it will return a decimal? type value. So, before using the Math.Round method, you could use Convert.ToDecimal() method to convert the average value. Please refer to the following code:

    AverageRating = Math.Round(Convert.ToDecimal((from pqr in db.PQuote where pqr.PQuoteRating != null select pqr.PQuoteRating).Average()));

    Best regards,
    Dillion

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, February 2, 2016 4:20 AM
  • User-745958333 posted

    Thanks guys - just checking....sorry for the delay.  My first test I get:

    Additional information: LINQ to Entities does not recognize the method 'System.Decimal ToDecimal(System.Object)' method, and this method cannot be translated into a store expression.

    Thursday, February 4, 2016 1:50 PM
  • User1577371250 posted

    Hi,

    did you try my query?

    Friday, February 5, 2016 5:48 AM
  • User-271186128 posted

    Hi Captain Planet,

    Additional information: LINQ to Entities does not recognize the method 'System.Decimal ToDecimal(System.Object)' method, and this method cannot be translated into a store expression.

    I suggest you could post the whole code about this LINQ query statement. In my previous reply the ToDecimal is outside the LINQ statement. Code like this:

    Var AverageRating = Math.Round(Convert.ToDecimal((from pqr in db.PQuote where pqr.PQuoteRating != null select pqr.PQuoteRating).Average()));
    
    

    The boldfaced words are the LINQ statement.

    Best regards,
    Dillion

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, February 5, 2016 9:16 AM