C# Dynamic linq order by expando object

2019-09-09 19:50发布

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)

1条回答
干净又极端
2楼-- · 2019-09-09 20:29

You could try with something like:

public static IOrderedEnumerable<Customer> OrderByCustomer(IEnumerable<Customer> enu, Column column)
{
    Func<Customer, object> selector;

    if (!column.ColName.StartsWith("custom_"))
    {
        var propertyInfo = typeof(Customer).GetProperty(column.ColName);
        selector = x => propertyInfo.GetValue(x, null);
    }
    else
    {
        selector = x =>
        {
            object obj;
            ((IDictionary<string, object>)x.custom).TryGetValue(column.ColName.Substring("custom_".Length), out obj);
            return obj;
        };
    }

    IOrderedEnumerable<Customer> ordered = enu as IOrderedEnumerable<Customer>;

    if (ordered == null)
    {
        if (column.SortOrder == SortOrder.Asc)
        {
            return enu.OrderBy(selector);
        }
        else
        {
            return enu.OrderByDescending(selector);
        }
    }

    if (column.SortOrder == SortOrder.Asc)
    {
        return ordered.ThenBy(selector);
    }
    else
    {
        return ordered.ThenByDescending(selector);
    }
}

use it like:

var sortedList = new[] { 
    new Column { ColName = "Name", SortOrder = SortOrder.Asc },
    new Column { ColName = "custom_Secret", SortOrder = SortOrder.Asc },
};

var col = sortedList[0];

var sortedData = OrderByCustomer(customers, sortedList[0]);

for (int i = 1; i < sortedList.Length; i++)
{
    sortedData = OrderByCustomer(sortedData, sortedList[i]);
}

var result = sortedData.ToArray();

Note that you use my OrderByCustomer both for the first ordering (where it uses the OrderBy/OrderByDescending) and for the other sub-ordering (where it uses the ThenBy/ThenByDescending)

查看更多
登录 后发表回答