You forgot the await.
return await x.ToListAsync();
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
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>
You forgot the await.
return await x.ToListAsync();
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?
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();
}