Now this is kind of dangerous to do since there is no compile time check (Like most things set in markup) but say you want to sort a collection, using the Linq extension methods, but you don’t know what you what to sort on at any given time. On top of that, you have a [...]
When is a field not a field?
Ok so for the last few things I’ve been showing a more dynamic approach to linq queries , mostly dealing with collections rather than say linq to sql. Now take the new method: public List<K> SelectFromUserList<K, L>(Func<User, K> selectMethod, Func<User, L> orderBy, List<User> userList) { List<K> userList = new List<K>(); userList = userList.OrderBy(orderBy).Select(selectMethod).ToList(); return userList; [...]
Adding to the Select
So in the last post there was something like this: public List<K> SelectUserNameList<K>(Func<User, K> selectMethod, List<User> userList) { return userList.Select(selectMethod, userList).ToList(); } Called by this: List<String> newList = userList.Select(user => user.Name, userList); Ok so what if you want to order something? Well same idea, just another Func. But remember, a Func that can order by [...]
I are tupid
This may be novel or really dumb, but I like it. Say you want to convert a Dictionary to a List of KeyValuePairs that are sorted by something within the dictionary Key or Value. Don’t ask why, just go with it. You could do this: Where someDictionary is Dictionary<Type, string> : List<KeyValuePair<Type, String>> dataSource = [...]