I'm floundering with this. A friend wrote this sometime ago and is now in another state so cannot help with expanding the idea.
// For index listing.
// Order function to identify sort direction.
private IOrderedEnumerable<Client> OrderClients<T>(IEnumerable<Client> clients, Func<Client, T> keySelector, SortDirection sortDirection) {
return sortDirection == SortDirection.desc
? clients.OrderByDescending(keySelector)
: clients.OrderBy(keySelector);
}
It works.
However my requirements have changed.
I now want a static method that can be supplied with an IEnumerable<T>
and a dictionary of <string, SortDirection>
where SortDirection
is the direction as an enum:
public enum SortDirection
{
asc = 0,
desc
}
This is because the Dictionary may have as many as 4 columns to sort by. The first value is always the orderBy
and all the others are thenBy
.
so I tried to start with the following method signature.
public static IOrderedEnumerable<T> OrderAndThenBy(IEnumerable<T> list, Func<T, Dictionary<string, SortDirection>>){}
I wanted to accept an IEnumerable and then use the dictionary with the columns to sort by "string" and the direction it should be sorted in.
This answer by Matthias seemed promising but at the heart of this I don't understand the mechanics of how the Func works as a parameter in order to use the dictionary of <key = columns, value = sortDirection>
on the IEnumerable<T>
(generic so I can use it on all tables that I require sorting) and then return an IOrderedEnumerable<T>
..
Could someone help with the structure of the method and also how to use the Func as a method parameter with one parameter of type Dictionary. At the moment I am getting an error on the "T"..
Error CS0246 The type or namespace name 'T' could not be found (are you missing a using directive or an assembly reference?)
because I have not written the generic method parameters correctly.. Hope someone might be able to help.