我想在lambda实体框架中对动态进行排序.我创建了更多的时间,但似乎不起作用.
////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();
}
Run Code Online (Sandbox Code Playgroud)
我尝试跟随
query = db.LOCATIONs.OrderByDescending(q => q.GetType().GetProperty(column_name).GetValue(q, null)).ToList();
Run Code Online (Sandbox Code Playgroud)
但是得到错误
LINQ to …Run Code Online (Sandbox Code Playgroud)