【发布时间】:2011-09-01 14:16:43
【问题描述】:
[更新]
对不起,我应该标记这个问题 作为 MVC-2,我将查询结果传递给 视图的模型,所以我必须指定类型 我的模型在视图的标题中 定义。我这样声明:
Inherits="System.Web.Mvc.ViewPage<IQueryable<dynamic>>"什么都没有改变,也没有 答案对我不起作用:(。 最后我使用了一个 ModelView 类作为 帮手将我的查询结果放入其中。 :(
[/update]
我有一个这样的查询:
IQueryable<dynamic> result = from d in KiaNetRepository.KiaNetEntities.Discounts
where d.AgentTypeID == agentTypeId
select new { d.Category, d.DiscountValue, d.PriceConfige };
然后我像这样在我的视图中检索值:
foreach(var item in result){
Category cat = item.Category; // throws exception 'object' does not contain a definition for 'Category'
//...
}
注意 IQueryable 的查询类型是匿名类...
【问题讨论】:
-
尝试探索
itemindebuger,我想你会在那里找到答案 -
调试器显示
item包含{ d.Category, d.DiscountValue, d.PriceConfige }成员!!!我很困惑! -
你需要
IQueryable<dynamic>吗?你不能只用IQueryable这个词吗?将其设置为dynamic将删除您的 IntelliSense,因为动态类型是为后期绑定而设计的。这也将帮助您解决为什么Category对象无效的问题。
标签: c# asp.net-mvc-2 dynamic anonymous-types iqueryable