So the “dynamic” linq query so far isn’t just enough to stop. Oh no, now that I can have methods pass back expressions, what about a dictionary of order by expressions to allow an even more dynamic feel? Huh? How’s about that kids? I hate myself too.
//Enum created for the dictionary key public enum OrderByChoice { FirstName, LastName, UserName } //dictionary of expressions private static Dictionary<OrderByChoice, Expression<Func<User, String>>> GetOrderByList() { if(orderByList == null) { orderByList = new Dictionary<OrderByChoice,Expression<Func<User, String>>>(); orderByList.Add(OrderByChoice.FirstName, currentUser => currentUser.FirstName); orderByList.Add(OrderByChoice.LastName, currentUser => currentUser.LastName); orderByList.Add(OrderByChoice.UserName, currentUser => currentUser.UserName); } return orderByList; }
That’s right, I know. Silly, but I think it’s cool. Now mind you, I could have methods that return expressions instead of the expressions themselves, but I wanted to show the expressions.
And for the method call:
public static IList<User> GetUserList(OrderByChoice orderBy) { return GetUserList(currentUser => true, GetOrderByList()[orderBy]).ToList(); }
And now you have an even more dynamicish call.
I FORGOT THE USINGS!!!!
using System; using System.Collections.Generic; using System.Linq; using System.Linq.Expressions;