none
Dynamicaly sort an IQueryable (LINQ to SQL) RRS feed

  • Question

  • Hi,

    I’m trying to sort a IQueryable dynamically based on the choice of the user.
    The user can choose up to 4 different fields from a list of 21 fields.
    For every sort field the user can indicate if the sorting needs to be ascending or descending.
    The choices of the user are stored in SortFields

     

    definition of SortFields:

     

    Public SortFields() As List(OfSortField)

     

    Class Sortfield has 3 properties:

     

    Public Property Column()As String

    Public Property Type()As Type

    Public Property Sort()As SortOrder

     

    My sorting procedure works and looks like this:

    Dim db As DataClasses1DataContext = DataClassesDataContext.GetInstance()

    Dim activities = From a In db.VW_Activities Select a

    If Not (activities Is Nothing OrElse activities.Count = 0) Then

       For nIndex = 0 To SortFields.Count - 1

          Select Case SortFields(nIndex).Column

             Case "FirstField"

               If SortFields(nIndex).Sort = Windows.Forms.SortOrder.Descending Then

                  activities = activities.OrderByDescending(Function(a) a.FirstField)

               Else

                  activities = activities.OrderBy(Function(a) a.FirstField)

               End If

             Case "SecondField"

               If SortFields(nIndex).Sort = Windows.Forms.SortOrder.Descending Then

                  activities = activities.OrderByDescending(Function(a) a.SecondField)

               Else

                  activities = activities.OrderBy(Function(a) a.SecondField)

               End If

     

    ' etc ...

    ' etc ...

    ' etc ...

      

             Case "LastField"

               If SortFields(nIndex).Sort = Windows.Forms.SortOrder.Descending Then

                  activities = activities.OrderByDescending(Function(a) a.LastField)

               Else

                  activities = activities.OrderBy(Function(a) a. LastField)

               End If

            End Select

         Next

    End If

     

    It is obvious that this is a very cumbersome way of working. I would like to know how I can write this more dynamic like this:

     

    Dim db As DataClasses1DataContext = DataClassesDataContext.GetInstance()

    Dim activities = From a In db.VW_Activities Select a

    If Not (activities Is Nothing OrElse activities.Count = 0) Then

       For nIndex = 0 To SortFields.Count - 1

          If SortFields(nIndex).Sort = Windows.Forms.SortOrder.Descending Then

            activities = activities.OrderByDescending(Function(a) a.SortFields(nIndex).Column)

          Else

            activities = activities.OrderBy(Function(a) a.SortFields(nIndex).Column)

          End If

       Next

    End If

     

    of course a.SortFields(nIndex).Column has no meaning and does not work

     

     

    Anybody an idea how I can do this?

    Thanks

    RuWel

    • Moved by Liliane Teng Wednesday, September 21, 2011 11:13 AM (From:Visual Basic General)
    Monday, September 19, 2011 11:22 AM

Answers

All replies