Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to sort a List<dynamic> containing ExpandoObjects

I have a list which contains a dictionary of ExpandoObjects. Im binding this to a grid but now I want to sort the list.

        var rows = new List<dynamic>();
        for (int i = 0; i < 1000; i++)
        {
            dynamic expandy = new ExpandoObject();
            var dictionary = (IDictionary<string, object>)expandy;

            dictionary.Add("ID", i);
            dictionary.Add("Name", "Name" + i);
            rows.Add(dictionary);
        }

So looking at the test code above how would I sort rows (ascending or decending) say on "ID" or "Name" or any other property that I dynamically add?

A bit more info, I'm looking to sort it like this (this doesnt work);

            var newOrder = from r in rows
                     orderby ("Name") ascending 
                     select r;
like image 635
WooHoo Avatar asked Mar 23 '12 14:03

WooHoo


1 Answers

Not sure how I missed this but anyway this works,

var newOrder = rows.OrderByDescending(x => x["Name"]);
like image 185
WooHoo Avatar answered Oct 01 '22 18:10

WooHoo