LINQ question in .NET 6 EF CORE conversion

Cloud Developer 1 Reputation point
2022-11-21T18:11:17.843+00:00

I currently have a .NET 4 WebAPI using Entity Framework 3 that I'm upgrading to .NET 6 EF CORE. I currently have a LINQ Query that looks like this (and works fine)

[HttpGet]  
public async Task<ActionResults> GetCars()  
{  
   var x = from f in _context.CarMakes  
   group c in f.Make into m  
   select new { c.Key };  
  
return Json(new   
{   
        data = await x  
        .ToListAsync()  
     };  
        
  
}  

this returns me:
Chevy
Ford
Volvo
Toyota

and so on.

I'm trying to use this same query in a .NET 6 WebAPI that is using EF CORE, it fails, and kicks back an error:

in the .NET 6 EF CORE project, I have:

[HttpGet]  
public async Task<ActionResults<IEnumerable<CarMakes>>>> GetCars()  
{  
   var x = from f in _context.CarMakes  
   group c in f.Make into m  
   select new { c.Key };  
  
return await x.ToListAsync();  
        
  
}  

I get an error message of: [ cannot implicity convert type 'System.Threading.Task.Task<System.Collections.GenericLis>> ] to Microsoft.AspNetCore.Mvc.ActionResults<System.Collections.Generic.IEnumerable<app.CarMakes>

Entity Framework Core
Entity Framework Core
A lightweight, extensible, open-source, and cross-platform version of the Entity Framework data access technology.
697 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,247 questions
{count} votes

3 answers

Sort by: Most helpful
  1. AgaveJoe 26,201 Reputation points
    2022-11-21T18:14:06.217+00:00

    You forgot the await.

    return await x.ToListAsync();  
    

  2. Cloud Developer 1 Reputation point
    2022-11-21T18:55:05.603+00:00

    I changed the code to this and it works:

       [HttpGet]  
        public async Task<ActionResults<IEnumerable<CarMakes>>>> GetCars()  
        {  
           var x = from f in _context.CarMakes  
           group c in f.League into m  
           order by c.key  
           select new { c.Key };  
      
     return Ok(await q.ToListAsync());  
                
      
        }  
    

    even with this, how can I grab the column name instead of the .Key?


  3. Bruce (SqlWork.com) 56,021 Reputation points
    2022-11-21T19:50:13.45+00:00

    you don't show you class definition, but assuming make is a string, then

       var x = from f in _context.CarMakes  
        group c in f.Make into m  
        select new { c.Key };  
    

    when the converted to json looks like:

    [{key: "Ford"}, {key: "Chevy", ...]

    not a list of string or a list of CarMakes, but a list of an anonymous objects. in net 6 this would be a list of objects not CarMakes:

    [HttpGet]  
     public async Task<ActionResults<IEnumerable<object>>> GetCars()  
     {  
        var x = from f in _context.CarMakes  
        group c in f.Make into m  
        select new { c.Key };  
          
        return await x.ToListAsync();  
     }