I have a situation where I will need to sort dynamically based on a configuration from database table ascending or descending
My Database table has column name That I will need to sort and sort order.
I have a list of objects, in which each object has an ExpandoObject.
My Class looks like this
public class Customer: Base {
public string Name { get; set;}
public string City { get; set;}
public string Address { get; set;}
public string State { get; set;}
public int Id { get; set;}
public DateTime Dob { get; set;}
public int Age{ get; set;}
public dynamic custom = new ExpandoObject();
}
Customer class has an ExpandoObject in order to store additional properties also called as custom properties which are configurable per Customer.
I have a table which has configurations for sorting, it has the Column names and sort order such as
ColName SortOrder
City Asc
Name Desc
custom_XYZ Asc //this means this is a custom column
All the column names starting with custom_(Property Name) are stored in expando object which in turn is a dictionary.
Right now I am using the below code to do dynamic linq orderBy
var sortList; // this list has the sorting list with type {string:ColName: bool:Asc}
var customers; //holds the list of customer objects with custom fields in expando
var col = sortList[0];
var propertyInfo = typeof(Student).GetProperty(col.ColName);
var sortedData= customers.OrderBy(x => propertyInfo.GetValue(x, null));
for(int i = 1; i<sortList.Count()-1;i++){
var col1 = sortList[i];
var prop = typeof(Student).GetProperty(col1.ColName);
if(col1.asc)
sortedData = sortedData.ThenBy(n => param2.GetValue(n, null));
else
sortedData = sortedData.ThenByDescending(n => param2.GetValue(n, null));
}
could someone help me on how could I sort data in custom object which is of type ExpandoObject.
ExpandoObject actually wraps the properties as a Dictionary internally so could someone help me on how could I sort the whole object based on the value of a Dictionary corresponding to a Key (which is columnName)