i have a student class.
class student : DynamicObject
{
public Dictionary<string, object> dics = new Dictionary<string, object>();
}
In am adding properties dynamically to this class.
After i populated a
list<student> lists,
How i can retrieve the keys and values in the dictionary in the each student class using linq. So that i can bind it to a datagrid. The keys will be same for all the dictionaries in each student object because that will be the column names. The values will different which are the column values. So that i can bind this anonymous type to the datagrid.
I have tried the following way. But the result is not correct.
var result = lists.SelectMany(x => x.Dictionary.Keys).Select(m => new
{
StudentID = m[1],
RegNo = m[2],
JoinYear = m[3]
}).ToList();
finally, i can bind like this,
grid.DataSource = result;
grid.DataBind();
So the grid will show the columns from the dictionary keys and all the rows will be populated using the dictionary values. Any help ???
Many thanks.
Your
SelectManyis unnecessary – you want to select one row for each student, not one row for each property on each student, so you only need aSelect:Of coure IMO you’d be far better off using strongly-typed properties on your
Studentobject and populating them properly from your stored procedure; the nyou wouldn’t need anonymous objects here at all.