我现在有一个使用实体框架3的.NET 4 Web,我正在升级到.NET 6/ EF。我现在有一个LINQ查询,它看起来像这样(并且工作正常):
[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()
};
}这将返回以下数据:
Chevy
Ford
Volvo
Toyota诸若此类。
我试图在使用error的ASP.NET Core6WebAPI中使用相同的查询,但是它失败了,并引发了一个错误。
在.NET 6/ EF核心项目中,我有:
[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();
}我收到一条错误消息:
'System.Threading.Task.Task类型
不能隐式转换
发布于 2022-11-21 18:28:50
不要返回ActionResult,只返回IEnumerable。
发布于 2022-11-21 19:03:39
我把这事做好了
[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 Ok(await x.ToListAsync());
}并不是100%肯定添加OK()会给我带来结果,但它现在起作用了。
https://stackoverflow.com/questions/74523171
复制相似问题