I want to use an anonymous type as source to a GridView. But it will be filtered first using a dropdownlist. Let me explain: I have a type I defined as follows:
IEnumerable<object> data = new[]{
new{Name="McClure Wallace", Id=1, Sal=10000d, Age=29},
new{Name="John Jones", Id=2, Sal=12000d, Age=27},
new{Name="Gloria Flowhart", Id=3,Sal=14000d, Age=21}
. . .
};
I use this as source to a dropdownlist:
ddlEmployee.DataSource=data;
ddlEmployee.DataTextField = "Name";
ddlEmployee.DataValueField = "Id";
ddlEmployee.DataBind();
When the user selects an item from the list I want to get the corresponding object and bind it in a Gridview:
var sel = ddlEmployee.SelectedItem.ToString();
var selData = from d in data where **d.Id= sel** select d;
gvSearchResults.DataSource = selData;
gvSearchResults.DataBind();
But the problem is that since I will get d as an object, it throws an error on d.Id.
How can I fix this. Please note that I may not be able to change definition of 'data' as this is controlled outside the app. I am only showing here for clarity.