无法将类型’System.Collections.Generic.List ‘隐式转换为’System.Linq.IQueryable ‘
我正在尝试在我的域服务(VS 2010 Silverlight业务应用程序)中创建一个查询,该查询返回作为特定值出现的检查读数的结果,我的数据库设置为:
Locations a) Inspections b) InspectionItems c) InspectionReadings a) Areas b) Inspections c) InspectionItems d) InspectionReadings
因此,正如您所看到的,区域和位置下的位置有检查读数。 我有一个名为StatusList的POCO:
public class StatusList { [Key] [Editable(false)] public Guid ID { get; set; } public string LocationName { get; set; } public DateTime LastInspectionDate { get; set; } public string Status { get; set; } }
我用它来返回查询的结果:
public IQueryable GetLocationStatus() { var status = (from location in this.ObjectContext.Locations where location.InspectionReadings.Status == value orderby a.DateTaken select new LocationStatusList() { ID = a.ID, LocationName = d.Name, }).ToList(); return status; }
不幸的是,它返回标题中的错误,我不知道为什么列表显然是一个列表项,我已经转换了结果
.ToList
问题正是因为你调用了ToList()
。 您已声明要返回IQueryable
,而List
未实现IQueryable
。
选项(选择一个):
- 删除
ToList
调用 - 将返回类型更改为
IEnumerable
,IList
或可能List
-
在
ToList()
之后调用AsQueryable()
ToList()
:... as before ... .ToList().AsQueryable();
请注意,在ToList
调用中不需要type参数 – 它与编译器推断的相同。