locked
Help with composing async Task<IEnumerable<variables>> ..... RRS feed

  • Question

  • User-507786106 posted

    I must select distinct values from one column named YEAR from the database table.  I MUST use async Task<IEnumerable<variables>> for this code in my class library.

    I need help with the body of the code in the class library. The goal is to return a list of Distinct Years from the database table.

    Code - need linq code.

    public async Task<IEnumerable<mClerkTbl>> GetFiscalYearList()

    {

          return await _mClerkTblRepository....... <-- HELP!!!

    }

    Monday, May 17, 2021 7:43 PM

Answers

  • User-474980206 posted

    old school:

    var result = await dbContext.TheTable
         .GroupBy(r => r.Year)
         .Select(grp => grp.FirstOrDefault())
         .ToListAsync();
    

    modern:

    var result = await dbContext.TheTable 
         .Select(r => r.Year) 
         .Distinct()
         .ToListAsync();

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, May 17, 2021 7:54 PM

All replies

  • User-474980206 posted

    old school:

    var result = await dbContext.TheTable
         .GroupBy(r => r.Year)
         .Select(grp => grp.FirstOrDefault())
         .ToListAsync();
    

    modern:

    var result = await dbContext.TheTable 
         .Select(r => r.Year) 
         .Distinct()
         .ToListAsync();

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, May 17, 2021 7:54 PM
  • User-507786106 posted

    Great job and thanks so much.  It worked perfectly!!!!

    Monday, May 17, 2021 9:00 PM