c# - Nerd Dinner pagginated list - how do I add a linq orderby clause dynamicaly? ANYONE? -
i have implemented nerd dinner method of paging- works great. want able dynamically able order certian fields.
how go implementing this. here code?
controller:
paginatedlist<classifieds_ads> pageofclassifieds = new paginatedlist<classifieds_ads>(classifiedsrepositry.getclassifiedsincategory(category), paging, 20);
repository:
return classifieds in context.classifieds_ads.include("user") (from catergory in context.classifieds_categories catergory.mvc_url == mvc_cat select catergory).contains(classifieds.classifieds_categories) orderby classifieds.dateposted descending select classifieds;
as can see have orderby clause "hard coded" repository. don't know code implement dynamically?
anybody have ideas?
thanks,
you can use orderby(of tsource, tkey)-extensionmethod , pass custom function via keyselector-parameter. maybe little example may give idea how start:
class { public string foo { get; set; } public int32 bar { get; set; } public override string tostring() { return foo + ":" + bar.tostring(); } } static void main(string[] args) { var x = new list<a> { new { foo = "abc", bar = 100 }, new a() { foo = "zzz", bar = 0 } }; func<a, string> order1 = (a) => a.foo; func<a, int32> order2 = (a) => a.bar; printquery(x, order1); console.writeline(); printquery(x, order2); console.readline(); } static void printquery<t>(ienumerable<a> query, func<a, t> orderfunc) { foreach (var e in query.orderby(orderfunc)) console.writeline(e); }
Comments
Post a Comment