How to sort a <dynamic> list containing ExpandoObjects

I have a list containing an ExpandoObjects dictionary. I snap this to the 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, let's look at the above test code, how can I sort the lines (increase or decrease), for example, β€œID” or β€œName” or any other property that I dynamically add?

A little more information, I'm looking to sort it like this (this does not work);

  var newOrder = from r in rows orderby ("Name") ascending select r; 
+4
source share
2 answers

Not sure how I missed this, but it works anyway,

 var newOrder = rows.OrderByDescending(x => x["Name"]); 
+4
source

WooHoo's answer does not work for Dynamic.ExpandoObject ; it works:

 var newOrder = rows.OrderByDescending(x =>((IDictionary<string,object>)x)["Name"]); 
+2
source

Source: https://habr.com/ru/post/1403100/


All Articles