我将我的产品表绑定(bind)到网格,如下所示
Action Controller
ViewData["Products"] = _repository.GetProducts(true);
查看
@(Html.Telerik().Grid((IEnumerable<Product>)ViewData["Products"])
一切正常。但我只需要从表中选择两个字段,并且我尝试使用下面的代码
ViewData["Products"] = _repository.GetProducts(true).Select(p => new
{
Id = p.Id,
Name = p.Name
});
现在我收到以下错误。也许有人可以解释一下 hot 来解决这个问题?
Unable to cast object of type System.Data.Linq.DataQuery to type System.Collections.Generic.IEnumerable
最佳答案
也许是这样的:
ViewData["Products"] = _repository.GetProducts(true).Select(p => new
KeyValuePair<int,string>
(
p.Id,
p.Name
)).ToList();
然后更改此:
@(Html.Telerik().Grid((List<KeyValuePair<int,string>>)ViewData["Products"])
关于asp.net-mvc-3 - Linq to SQL 无法转换对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9975951/