简体   繁体   English

C#显示ExpandoObject的数据-无法将lambda表达式转换为类型“…”,因为它不是委托

[英]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.

相关问题 将VB.NET代码转换为C#:无法将Lambda表达式转换为类型“ Delegate”,因为它不是委托类型 - Converting VB.NET code to C#: Cannot convert lambda expression to type 'Delegate' because it is not a delegate type C#无法将Lambda表达式转换为类型“ Delegate”,因为它不是委托类型 - C# Cannot convert lambda expression to type 'Delegate' because it is not a delegate type “无法将lambda表达式转换为&#39;string&#39;类型,因为它不是委托类型”在C#中查询数据集 - “Cannot convert lambda expression to type 'string' because it is not a delegate type” querying dataset in C# C# 无法将 lambda 表达式转换为“动态”类型,因为它不是委托类型 - C# Cannot convert lambda expression to type 'dynamic' because it is not a delegate type C# 链表查找方法抛出错误“无法将 lambda 表达式转换为类型‘Item’,因为它不是委托类型” - C# linkedlist find method throws error "cannot convert lambda expression to type 'Item' because it is not a delegate type" C# Linq 与 MVC,无法将 lambda 表达式转换为类型“字符串”,因为它不是委托类型 - C# Linq with MVC, Cannot convert lambda expression to type 'string' because it is not a delegate type 无法将Lambda表达式转换为类型,因为它不是委托 - Cannot convert lambda expression to type because it is not a delegate 无法将lambda表达式转换为委托类型,因为委托不是类型 - Cannot convert lambda expression to type delegate because delegate is not a type 无法将Lambda表达式转换为类型“ Delegate”,因为它不是委托类型MVC 5 - Cannot convert lambda expression to type 'Delegate' because it is not a delegate type MVC 5 无法将lambda表达式转换为“委托”类型,因为它不是委托类型 - Cannot convert lambda expression to type 'Delegate' because it is not a delegate type
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM