locked
how to get the max value from the linq statement, Please help RRS feed

  • Question

  • User-1355965324 posted

    Hi

    how can I make the following linq statement to get maximum value of reading for the year and month and depot, Any help would be very appreciated

    public long GetMaxMeterReading(int depotno, int year, int month)   // 
            {
                long maxRead = (from d in ctx.goMeterReading
                                where d.DepotNo == depotno && d.ReadingYear == year && d.ReadingMonth == month
                                select d.Reading).SingleOrDefault();
                return maxRead;
            }

    Regards

    Pol

    Friday, November 8, 2019 4:00 PM

Answers

  • User1634355159 posted

    Hi polachan,

    You could use this and make sure your "Reading" can be sorted:

    public long GetMaxMeterReading(int depotno, int year, int month)   // 
            {
                var maxRead = (from d in _context.goMeterReadings
                               where d.DepotNo == depotno && d.ReadingYear == year && d.ReadingMonth == month
                               select d.Reading).ToList().OrderByDescending(n => n).FirstOrDefault();
                                
                return maxRead;
            }

    or

    public long GetMaxMeterReading(int depotno, int year, int month)   // 
            {
                var maxRead = (from d in _context.goMeterReadings
                               where d.DepotNo == depotno && d.ReadingYear == year && d.ReadingMonth == month
                               orderby d.Reading descending
                               select d.Reading  ).FirstOrDefault();
    
                return maxRead;
            }

    Best Regards,

    Lewis

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, November 11, 2019 9:13 AM

All replies

  • User-474980206 posted
    var maxRead = (
       from d in ctx.goMeterReading
       where d.DepotNo == depotno 
           && d.ReadingYear == year 
           && d.ReadingMonth == month
       select d.Reading
       orderby d.Reading descending
    ).FirstOrDefault();
    

    Friday, November 8, 2019 4:41 PM
  • User-1355965324 posted

    I applied the following it is working fine. will it make any slow in future, Please can you advise me

    int maxReading = (from d in ctx.goMeterReading.Local
    where d.DepotNo == depotno && d.ReadingYear == year && d.ReadingMonth == month
    select d.Reading).Max();
    return maxReading;

    Friday, November 8, 2019 4:49 PM
  • User-1355965324 posted

    When I apply the given statement the following error ' Type or namespace could not be found'

     var maxRead = (  from d in ctx.goMeterReading   where d.DepotNo == depotno   && d.ReadingYear == year   && d.ReadingMonth == month
                                 select d.Reading
                                 orderby d.Reading descending).FirstOrDefault();

    Friday, November 8, 2019 4:53 PM
  • User1634355159 posted

    Hi polachan,

    You could use this and make sure your "Reading" can be sorted:

    public long GetMaxMeterReading(int depotno, int year, int month)   // 
            {
                var maxRead = (from d in _context.goMeterReadings
                               where d.DepotNo == depotno && d.ReadingYear == year && d.ReadingMonth == month
                               select d.Reading).ToList().OrderByDescending(n => n).FirstOrDefault();
                                
                return maxRead;
            }

    or

    public long GetMaxMeterReading(int depotno, int year, int month)   // 
            {
                var maxRead = (from d in _context.goMeterReadings
                               where d.DepotNo == depotno && d.ReadingYear == year && d.ReadingMonth == month
                               orderby d.Reading descending
                               select d.Reading  ).FirstOrDefault();
    
                return maxRead;
            }

    Best Regards,

    Lewis

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, November 11, 2019 9:13 AM
  • User438705957 posted

    Try this

    var reads = from d in ctx.goMeterReading
                                where d.DepotNo == depotno && d.ReadingYear == year && d.ReadingMonth == month
                                select d.Reading;

    long maxRead = reads.Max(r => r.Read)

    Tuesday, November 19, 2019 4:42 AM