【发布时间】:2018-08-14 10:24:42
【问题描述】:
我正在使用 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;
}
我收到以下错误:
错误 CS0266 无法将类型“System.Collections.Generic.List”隐式转换为“System.Collections.Generic.IEnumerable”。存在显式转换(您是否缺少演员表?)**
铸造是如何可能的?我已将 IEnumerable 更改为 IList,但问题存在。我不想为此创建 ViewModel。
【问题讨论】:
-
你的函数应该返回
IEnumerable<Strings>,但你返回的是一个匿名对象的集合.. -
这里的Strings是一个模型类。
-
而不是
.Select(x => new {...(匿名对象),使用.Select(x => new Strings {....
标签: c# linq asp.net-web-api