C# show data of ExpandoObject - Cannot convert lambda expression to type '…' because it is not a delegate

mrfazolka

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... :

@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

How can I add columns to grid to show data of ExpandoObject?

xanatos

You can't do that. IGridColumnCollection.Add has as a parameter an Expression tree:

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)

(I had already thought of using dynamic for your other question :-) )

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. But you can try :-)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related