Hi guys,
I'm trying to build a query using linq-to-sql and, in order to provide sorting capabilities, I wrote something like this:
private Dictionary<string, Expression<Func<DataAccess.Auditing, object>>> OrderDict { get; set; }
OrderDict = new Dictionary<string,Expression<Func<Augeos.GereonWeb.DataAccess.Auditing, object>>>();
OrderDict.Add("Date", p => (DateTime)p.RequestDateTime);
OrderDict.Add("Username", p => (string)p.CdUsername);
OrderDict.Add("Portfolio", p => (string)p.CdPortfolio);
OrderDict.Add("Url", p => (string)p.RequestedUrl);
OrderDict.Add("Result", p => (bool)p.RequestResult);
OrderDict.Add("Duration", p => (float)p.RequestDuration);
private IQueryable<DataAccess.Auditing> SetOrder(string orderBy, bool orderDirection, IQueryable<DataAccess.Auditing> query)
{
if (orderDirection)
return query.OrderByDescending(OrderDict[orderBy]);
else
return query.OrderBy(OrderDict[orderBy]);
}
My goal is to use the SortOrder function to sort the query. The main problem is that Func returns an object and linq cannot sort elements of this type. I truly need to use object as a return type because I have to sort by a wide range of types. Is it actually possible to slightly modify this code and make it to work?
Thank you,
Gio