Voglio ordinare la dinamica in framework di entità lambda. L'ho fondata più tempo, ma sembra non funzionare.Come ordinare dinamici nel framework di entità 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();
}
provo con un follow
query = db.LOCATIONs.OrderByDescending(q => q.GetType().GetProperty(column_name).GetValue(q, null)).ToList();
ma ottenere errore 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
Puoi dirmi qualche errore o sbagliato e come rimediare? Grazie mille.
http://stackoverflow.com/questions/31955025/generate-ef-orderby-expression-by-string – Shyju
ho provato, ma non è un lavoro. –
@BrianCrist Si prega di essere più specifico. * Cosa * non funziona? Penso che dovresti semplicemente eseguire "OrderBy (column_name)", assumendo che tu stia usando Dynamic LINQ. –