问题描述
我想在我的域名服务(VS 2010 Silverlight业务应用程序),返回从出来作为一个特定的值检查读数结果来创建一个查询,我的数据库设置为:
位置
A)检查
B)InspectionItems
C)InspectionReadings
A)地区
B)检查
C)InspectionItems
D)InspectionReadings
所以,你可以看到,有检查读数根据区域和地点的位置。我有一个POCO叫名字StatusList:
公共类StatusList
{
[关键]
〔编辑(假)]
公众的Guid ID {搞定;组; }
公共字符串LOCATIONNAME {搞定;组; }
公众的DateTime LastInspectionDate {搞定;组; }
公共字符串状态{搞定;组; }
}
而我使用返回查询的结果:
公开的IQueryable< StatusList> GetLocationStatus()
{
VAR状态=(从this.ObjectContext.Locations
位置,location.InspectionReadings.Status ==价值
排序依据a.DateTaken
选择新LocationStatusList()
{
ID = a.ID,
LOCATIONNAME = d.Name,
})了ToList< StatusList>();
返回状态;
}
不幸的是,返回错误的标题,我不知道为什么作为名单显然是一个列表项,我已经转换的结果。
.ToList< LocationStatusList>
问题恰恰的,因为你叫了ToList()
。你已经宣布你返回的IQueryable< LocationStatusList>
和列表< T>
不执行的IQueryable< T>
选项(任选其一):
- 删除
了ToList
呼叫 - 更改返回类型为
的IEnumerable< ; LocationStatusList>
,的IList< LocationStatusList>
或可能列表< LocationStatusList>
-
呼叫
AsQueryable已()
在了ToList()
:...像以前那样...
.ToList()AsQueryable已()。
请注意,你并不需要的类型在了ToList
调用的参数 - 这是同一个编译器会推断反正
。
I am trying to create a query in my domain service (VS 2010 Silverlight Business Application) that returns the results from inspection readings that came out as a specific value, my database is set up as:
Locations
a) Inspections
b) InspectionItems
c) InspectionReadings
a) Areas
b) Inspections
c) InspectionItems
d) InspectionReadings
So, as you can see, there are inspection readings for locations under areas and locations. I have a POCO called name StatusList:
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; }
}
which I am using to return the results of the query:
public IQueryable<StatusList> 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<StatusList>();
return status;
}
unfortunately, it's returning the error in the title and I have no idea why as the list is clearly a list item and i have converted the results
.ToList<LocationStatusList>
The problem is precisely because you've called ToList()
. You've declared that you're returning IQueryable<LocationStatusList>
, and List<T>
doesn't implement IQueryable<T>
.
Options (pick one):
- Remove the
ToList
call - Change the return type to
IEnumerable<LocationStatusList>
,IList<LocationStatusList>
or possiblyList<LocationStatusList>
Call
AsQueryable()
afterToList()
:... as before ... .ToList().AsQueryable();
Note that you don't need the type argument in the ToList
call - it's the same one that the compiler would infer anyway.
这篇关于无法隐式转换类型'System.Collections.Generic.List< T>'以“System.Linq.IQueryable< T>'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!