Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. LINQ: How To Make Order By Conditional?

LINQ: How To Make Order By Conditional?

Scheduled Pinned Locked Moved Visual Basic
csharpdatabaselinqtutorialquestion
8 Posts 4 Posters 4 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Alan Burkhart
    wrote on last edited by
    #1

    I'm trying to figure out how to make the Ascending / Descending extension in an Order By statement in a Linq query conditional based upon a menu selection. Here's the query:

    Dim fileItem = From files In itemList
    Where files.Tag(0) = "file"
    Order By files.SubItems.Item(indx).Text 'Ascending or Descending dependent upon menu selection
    Select files

    (itemList is a List of listviewitems) Any advice is appreciated.

    Sometimes the true reward for completing a task is not the money, but instead the satisfaction of a job well done. But it's usually the money.

    L Richard DeemingR 2 Replies Last reply
    0
    • A Alan Burkhart

      I'm trying to figure out how to make the Ascending / Descending extension in an Order By statement in a Linq query conditional based upon a menu selection. Here's the query:

      Dim fileItem = From files In itemList
      Where files.Tag(0) = "file"
      Order By files.SubItems.Item(indx).Text 'Ascending or Descending dependent upon menu selection
      Select files

      (itemList is a List of listviewitems) Any advice is appreciated.

      Sometimes the true reward for completing a task is not the money, but instead the satisfaction of a job well done. But it's usually the money.

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      See Linq Order By When You Have Property Name - DaedTech[^].

      A 1 Reply Last reply
      0
      • L Lost User

        See Linq Order By When You Have Property Name - DaedTech[^].

        A Offline
        A Offline
        Alan Burkhart
        wrote on last edited by
        #3

        Not exactly what I was hunting since the menu item isn't a part of the collection. But no big thing. I finally just added code to reverse the collection if I needed it sorted in descending order.

        Sometimes the true reward for completing a task is not the money, but instead the satisfaction of a job well done. But it's usually the money.

        1 Reply Last reply
        0
        • A Alan Burkhart

          I'm trying to figure out how to make the Ascending / Descending extension in an Order By statement in a Linq query conditional based upon a menu selection. Here's the query:

          Dim fileItem = From files In itemList
          Where files.Tag(0) = "file"
          Order By files.SubItems.Item(indx).Text 'Ascending or Descending dependent upon menu selection
          Select files

          (itemList is a List of listviewitems) Any advice is appreciated.

          Sometimes the true reward for completing a task is not the money, but instead the satisfaction of a job well done. But it's usually the money.

          Richard DeemingR Offline
          Richard DeemingR Offline
          Richard Deeming
          wrote on last edited by
          #4

          Simple:

          Dim fileItem = From files In itemList
          Where files.Tag(0) = "file"
          Select files

          If shouldSortDescending Then
          fileItem = From files In fileItem
          Order By files.SubItems.Item(indx).Text Descending
          Select files
          Else
          fileItem = From files In fileItem
          Order By files.SubItems.Item(indx).Text
          Select files
          End If

          Or, using "method syntax":

          Dim fileItem = itemList.Where(Function (files) files.Tag(0) = "file")

          If shouldSortDescending Then
          fileItem = fileItem.OrderByDescending(Function (files) files.SubItems.Item(indx).Text)
          Else
          fileItem = fileItem.OrderBy(Function (files) files.SubItems.Item(indx).Text)
          End If


          "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

          "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

          M A 2 Replies Last reply
          0
          • Richard DeemingR Richard Deeming

            Simple:

            Dim fileItem = From files In itemList
            Where files.Tag(0) = "file"
            Select files

            If shouldSortDescending Then
            fileItem = From files In fileItem
            Order By files.SubItems.Item(indx).Text Descending
            Select files
            Else
            fileItem = From files In fileItem
            Order By files.SubItems.Item(indx).Text
            Select files
            End If

            Or, using "method syntax":

            Dim fileItem = itemList.Where(Function (files) files.Tag(0) = "file")

            If shouldSortDescending Then
            fileItem = fileItem.OrderByDescending(Function (files) files.SubItems.Item(indx).Text)
            Else
            fileItem = fileItem.OrderBy(Function (files) files.SubItems.Item(indx).Text)
            End If


            "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

            M Offline
            M Offline
            Mycroft Holmes
            wrote on last edited by
            #5

            Here Alan was looking for some Linq magic (so was I) and you give him a simple if statement. :laugh: :laugh: :laugh: KISS is alive and well in CP!

            Never underestimate the power of human stupidity RAH

            Richard DeemingR A 2 Replies Last reply
            0
            • M Mycroft Holmes

              Here Alan was looking for some Linq magic (so was I) and you give him a simple if statement. :laugh: :laugh: :laugh: KISS is alive and well in CP!

              Never underestimate the power of human stupidity RAH

              Richard DeemingR Offline
              Richard DeemingR Offline
              Richard Deeming
              wrote on last edited by
              #6

              You could always create your own extension method. :)

              Module SortingExtensions
              <Extension()>
              Public Function OrderBy(Of TElement, TKey)(
              ByVal source As IEnumerable(Of TElement),
              ByVal selector As Func(Of TElement, TKey),
              ByVal sortDescending As Boolean) _
              As IOrderedEnumerable(Of TElement)

                  Return If(sortDescending, source.OrderByDescending(selector), source.OrderBy(selector))
              End Function
              
              <Extension()> 
              Public Function ThenBy(Of TElement, TKey)(
                  ByVal source As IOrderedEnumerable(Of TElement), 
                  ByVal selector As Func(Of TElement, TKey), 
                  ByVal sortDescending As Boolean) \_
                  As IOrderedEnumerable(Of TElement)
                  
                  Return If(sortDescending, source.ThenByDescending(selector), source.ThenBy(selector))
              End Function
              

              End Module

              ...

              Dim fileItem = itemList.Where(Function (files) files.Tag(0) = "file").OrderBy(Function (files) files.SubItems.Item(indx).Text, shouldSortDescending)

              There's no option to extend the query syntax though.


              "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

              "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

              1 Reply Last reply
              0
              • M Mycroft Holmes

                Here Alan was looking for some Linq magic (so was I) and you give him a simple if statement. :laugh: :laugh: :laugh: KISS is alive and well in CP!

                Never underestimate the power of human stupidity RAH

                A Offline
                A Offline
                Alan Burkhart
                wrote on last edited by
                #7

                :thumbsup::thumbsup::thumbsup:

                Sometimes the true reward for completing a task is not the money, but instead the satisfaction of a job well done. But it's usually the money.

                1 Reply Last reply
                0
                • Richard DeemingR Richard Deeming

                  Simple:

                  Dim fileItem = From files In itemList
                  Where files.Tag(0) = "file"
                  Select files

                  If shouldSortDescending Then
                  fileItem = From files In fileItem
                  Order By files.SubItems.Item(indx).Text Descending
                  Select files
                  Else
                  fileItem = From files In fileItem
                  Order By files.SubItems.Item(indx).Text
                  Select files
                  End If

                  Or, using "method syntax":

                  Dim fileItem = itemList.Where(Function (files) files.Tag(0) = "file")

                  If shouldSortDescending Then
                  fileItem = fileItem.OrderByDescending(Function (files) files.SubItems.Item(indx).Text)
                  Else
                  fileItem = fileItem.OrderBy(Function (files) files.SubItems.Item(indx).Text)
                  End If


                  "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                  A Offline
                  A Offline
                  Alan Burkhart
                  wrote on last edited by
                  #8

                  Thanks Richard. I ended up writing something similar to the "Simple" version. I've been shamelessly flip-flopping around on this thing for a month - good thing it's just for me and not a customer :-D.

                  Sometimes the true reward for completing a task is not the money, but instead the satisfaction of a job well done. But it's usually the money.

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups