[英]C# show data of ExpandoObject - Cannot convert lambda expression to type '…' because it is not a delegate
I have this method: 我有这种方法:
public ActionResult dataGrid()
{
List<ExpandoObject> list = new List<ExpandoObject>();
using (OdbcCommand DbCommand = repODBC.dbConnection.CreateCommand())
{
DbCommand.CommandText = "Select * From MyTable";
OdbcDataReader reader = DbCommand.ExecuteReader();
while (reader.Read())
{
dynamic obj = new ExpandoObject();
obj.name="Peter";
obj.num=123;
obj.id=1;
list.Add(obj);
}
return View(list);
}
}
I try to access data this way. 我尝试以这种方式访问数据。 I use Grid.Mvc... : 我使用Grid.Mvc ...:
@Html.Grid((IEnumerable<System.Dynamic.ExpandoObject>)Model).Columns(columns =>
{
columns.Add(m => m.id).Titled("ID");
columns.Add(m => m.name).Titled("Name");
columns.Add(m => m.num).Titled("OS").Filterable(true);
})
but i get this error: Cannot convert lambda expression to type 'GridMvc.Columns.IGridColumn' because it is not a delegate 但我收到此错误: 无法将lambda表达式转换为类型'GridMvc.Columns.IGridColumn',因为它不是委托
How can I add columns to grid to show data of ExpandoObject? 如何在网格中添加列以显示ExpandoObject的数据?
You can't do that. 你不能那样做。 IGridColumnCollection.Add
has as a parameter an Expression tree: IGridColumnCollection.Add
具有一个表达式树作为参数:
IGridColumn<T> Add<TKey>(Expression<Func<T, TKey>> constraint)
dynamic
doesn't mix well with expression trees "built by the compiler" (by "built by the compiler" I mean expression trees not built manually through the Expression
class but built directly in source code) dynamic
与“由编译器构建”的表达式树不能很好地混合(通过“由编译器构建”,我的意思是表达式树不是通过Expression
类手动构建,而是直接在源代码中构建)
(I had already thought of using dynamic
for your other question :-) ) (我已经考虑过将dynamic
用于您的其他问题:-))
Now... You could try using: 现在...您可以尝试使用:
columns.Add(m => ((IDictionary<string, object>)m)["id"]).Titled("ID");
(an ExpandoObject
implements (privately) the interface IDictionary<string, object>
), but 50% it won't work and you'll get an exception at runtime. ( ExpandoObject
(私有地)实现IDictionary<string, object>
接口),但是50%的接口将无法工作,并且在运行时会出现异常。 But you can try :-) 但是你可以尝试:-)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.