c# - Dictionary of Linq Expression -
possible duplicate:
declaring func<in t,out result> dynamically
i'm trying build query using linq-to-sql and, in order provide sorting capabilities, wrote 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 use sortorder function sort query. main problem func returns object , linq cannot sort elements of type. need use object return type because have sort wide range of types. possible modify code , make work?
thank you,
gio
hm you'd have go low-level in expression tree of query such thing. easier way use dynamiclinq library, can .orderby() column string value. documentation can found here on scott guthrie's blog.
Comments
Post a Comment