locked
Sequence contain bo elements error please help RRS feed

  • Question

  • User-1355965324 posted

    From the  sql the following error is coming , when trying to find out max number and  If there is no any record on the table  , then this LINQ sql error  is coming 'Sequence  contain bo elements' if no record the return value should be 0

    public int GetMaxMeterReading(int depotno, int year,string metercode)   // 
            {
                int maxReading = (from d in ctx.goMeterReading
                                  where d.DepotNo == depotno && d.ReadingYear == year && d.MeterCode == metercode
                                  select d.Reading).Max();
                return maxReading;
    
    
            }

    Tuesday, November 12, 2019 4:34 PM

Answers

  • User475983607 posted

    From the  sql the following error is coming , when trying to find out max number and  If there is no any record on the table  , then this LINQ sql error  is coming 'Sequence  contain bo elements' if no record the return value should be 0

    Write code to check if the LINQ query returns results.  

    var readings = (from d in ctx.goMeterReading
    	  where d.DepotNo == depotno && d.ReadingYear == year && d.MeterCode == metercode
    	  select d.Reading).ToList();
    	  
    int maxReading = 0;	  
    if(readings != null && readings.Count() > 0)
    {
    	maxReading = readings.Max();
    }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, November 12, 2019 5:16 PM
  • User711641945 posted

    Hi polachan,

    You could also insert the DefaultIfEmpty() command between the query and Max() to prevent this error:

    int maxReading = (from d in ctx.goMeterReading
                      where d.DepotNo == depotno && d.ReadingYear == year && d.MeterCode == metercode
                      select d.Reading).DefaultIfEmpty(0).Max();

    Best Regards,

    Rena

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, November 13, 2019 4:31 AM

All replies

  • User475983607 posted

    From the  sql the following error is coming , when trying to find out max number and  If there is no any record on the table  , then this LINQ sql error  is coming 'Sequence  contain bo elements' if no record the return value should be 0

    Write code to check if the LINQ query returns results.  

    var readings = (from d in ctx.goMeterReading
    	  where d.DepotNo == depotno && d.ReadingYear == year && d.MeterCode == metercode
    	  select d.Reading).ToList();
    	  
    int maxReading = 0;	  
    if(readings != null && readings.Count() > 0)
    {
    	maxReading = readings.Max();
    }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, November 12, 2019 5:16 PM
  • User711641945 posted

    Hi polachan,

    You could also insert the DefaultIfEmpty() command between the query and Max() to prevent this error:

    int maxReading = (from d in ctx.goMeterReading
                      where d.DepotNo == depotno && d.ReadingYear == year && d.MeterCode == metercode
                      select d.Reading).DefaultIfEmpty(0).Max();

    Best Regards,

    Rena

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, November 13, 2019 4:31 AM
  • User-1355965324 posted

    Many Thanks to all

    Wednesday, November 13, 2019 8:59 AM