Conditional OrderBy

Posted by jeriley on Stack Overflow See other posts from Stack Overflow or by jeriley
Published on 2010-06-17T17:27:08Z Indexed on 2010/06/17 17:33 UTC
Read the original article Hit count: 575

Filed under:
|
|

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?

© Stack Overflow or respective owner

Related posts about vb.net

Related posts about LINQ