Steve Jobs Steve Jobs - 1 year ago 78
SQL Question

how to order by a dynamic column name in EntityFramework?

I am trying to get following code working , This was working fine for MSSQL , but since i changed to use mySql it is not working

records.Content = db.areas
.Where(x => x.Name.Contains(filter)))
.OrderBy("dated desc")

I get the error " Unable to load one or more of the requested types. Retrieve the LoaderExceptions property for more information."

string colName = "datedD" ;

how to order by depneding on colName variable ?


Answer Source

Try this

string filterString = "dated";
bool isAscSorting = false;

Func<dynamic, dynamic> orderingFunction = i =>
                                filterString == "dated" ? i.dated :
                                filterString == "something" ? i.columnx : "";

records.Content = (isAscSorting) ?
                         .Where(x =>   x.Name.Contains(filter)))
                         .Where(x =>   x.Name.Contains(filter)))
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download