无法隐式转换类型'System.Collections.Generic.List <>'

Par*_*tha -3 c# linq asp.net-web-api

我正在使用VS 2017来开发我的.NET应用程序.我编写了以下代码来从api中的表中检索数据

[HttpGet]
[Route("Index")]
public IEnumerable<Strings> Index()
{
    var list = db.Strings.Select(x => new { x.Iid, x.Value, x.Description, x.Itype }).ToList();
    return list;
}
Run Code Online (Sandbox Code Playgroud)

我收到以下错误:

错误CS0266无法将类型'System.Collections.Generic.List <>'隐式转换为'System.Collections.Generic.IEnumerable'.存在显式转换(您是否错过了演员?)**

如何铸造是可能的?我已将IEnumerable更改为IList但问题仍然存在.我不想为此创建ViewModel.

stu*_*rtd 8

您正在创建一个匿名对象列表,Select(x => new …)然后尝试将其作为一个返回IEnumerable<Strings>,这就是您收到错误的原因.

无论如何都不能从方法返回匿名对象(1),因此要么更改查询以返回列表,Strings要么使用中间对象来表示数据.

(1)嗯,你可以,但不是一个可用的形式.