1

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.

Josh Darnell
  • 11,304
  • 9
  • 38
  • 66
Rupesh Saini
  • 85
  • 1
  • 7
  • possible duplicate of [Accessing C# Anonymous Type Objects](http://stackoverflow.com/questions/713521/accessing-c-sharp-anonymous-type-objects) – nawfal Jun 28 '14 at 08:46

3 Answers3

3

Your problem is that you're using Enumerable<object>. When you do your LINQ query it will say that Id is not part of object

Try this instead

var 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}
        . . .
};

Also, I don't know if d.Id= sel is exaclty what you have (except for the *). If that's the case you need to change a couple of things

1) sel is a string so you need to convert it to int

2) you need to use == to compare

Claudio Redi
  • 67,454
  • 15
  • 130
  • 155
  • Perfect!! Exactly what I was looking for! – Rupesh Saini Jun 07 '12 at 13:41
  • I have another question related to this: Can I pass and retrieve the "data" through a Session Object? What should I use for casting? – Rupesh Saini Jun 08 '12 at 07:54
  • @Rupesh Saini: that's a little more complicated and not very recommended, a defined type is prefered for this case. You might be able to do it using something called "cast by example". see this related question http://stackoverflow.com/questions/1409734/cast-to-anonymous-type – Claudio Redi Jun 08 '12 at 12:26
1

You can use "dynamic" data type, so something like this works:

        dynamic 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}
        };

        Console.WriteLine(data[0].Name);

Would this help you?

walther
  • 13,466
  • 5
  • 41
  • 67
1

First, you want sel to be the SelectedValue, not the SelectedItem (so that you get the id property for your where clause). You also want it to be an int, so that you can compare it to the id in your LINQ query:

int sel = int.Parse(ddlEmployee.SelectedValue);

Next, you can set your IEnumerable as a dynamic typed object, to avoid the compiler errors you mentioned:

IEnumerable<dynamic> 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}
                                };

Finally (as Caludio said), you do want to use the == operator in your LINQ query:

var selData = from d in data where d.Id == sel select d;

At this point, your DataBind() should run with no problems.

Community
  • 1
  • 1
Josh Darnell
  • 11,304
  • 9
  • 38
  • 66
  • Thanks for your suggestion. Unfortunately I am struck with 3.5 so I cant try your solution but I am sure it works so +1 for you – Rupesh Saini Jun 07 '12 at 13:40
  • @RupeshSaini Ah, I always forget `dynamic` is a 4.0 feature. Good luck, I'm glad you were able to get some help! – Josh Darnell Jun 07 '12 at 14:29