I want to sort a dynamic structure into lambda entities. I founded it more time, but it doesn't seem to work.
////string column_name // the name of column in table <<< don't care this, I finished ////string sort_order // ASC or DESC <<< don't care this, I finished using (var db = new ABCEntities()) { // get dynamic type of column name , but i can not // ??????????? var columnExp = typeof(LOCATION).GetProperty(column_name); IEnumerable<LOCATION> query = db.LOCATIONs; if(sort_order = "ASC") { query = query.OrderBy(columnExp).Tolist(); } else query = query.OrderByDescending(columnExp).Tolist(); }
I try to follow
query = db.LOCATIONs.OrderByDescending(q => q.GetType().GetProperty(column_name).GetValue(q, null)).ToList();
But get the error in
LINQ to Entities does not recognize the method 'System.Object GetValue(System.Object, System.Object[])' method, and this method cannot be translated into a store expression
Can you tell me some mistake or not and how to fix it? Thank you very much.
sorting c # lambda sql-order-by entity-framework
Brian crist
source share