Conditional OrderBy
- by jeriley
So, right now I've got a number of columns the user can sort by (Name, County, Active) and that's easy but messy. Looks something like this...
Select Case e.SortExpression
Case "Name"
If (isDescending) Then
resultsList.OrderByDescending(Function(a) a.Name).ToList()
Else
resultsList.OrderBy(Function(a) a.Name).ToList()
End If
Case "County" ... and so on
what I would LIKE to do, is something more ... graceful, like this
Private Function SortThatList(ByVal listOfStuff As List(Of Stuff), ByVal isDescending As Boolean, ByVal expression As Func(Of Stuff)) As List(Of Stuff)
If (isDescending) Then
Return listOfStuff.OrderByDescending(expression)
Else : Return listOfStuff.OrderBy(expression)
End If
End Function
but it doesn't like the datatype (Of TKey) ... I've tired Func(Of stuff, boolean) (got something in c# that works nicely like that) but can't seem to get this one to do what I want. Ideas? What's the magic syntax?