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. LINQ
  4. LINQ to Entities - multiple OrderBy methods do not work

LINQ to Entities - multiple OrderBy methods do not work

Scheduled Pinned Locked Moved LINQ
csharpdatabaselinqhelpquestion
4 Posts 3 Posters 9 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.
  • D Offline
    D Offline
    Dmitry Otblesk
    wrote on last edited by
    #1

    If I apply 2 OrderBy methods to my query, like that

    query.OrderBy(rec => rec.Name).OrderByDescending(rec => rec.Title);

    then only second method is applied, the first one is ignored. Is it a bug? What if I need to have ascending ordering for one column and descending ordering for another? Is it not achievable at all by method syntax?

    K N D 3 Replies Last reply
    0
    • D Dmitry Otblesk

      If I apply 2 OrderBy methods to my query, like that

      query.OrderBy(rec => rec.Name).OrderByDescending(rec => rec.Title);

      then only second method is applied, the first one is ignored. Is it a bug? What if I need to have ascending ordering for one column and descending ordering for another? Is it not achievable at all by method syntax?

      K Offline
      K Offline
      Keith Barrow
      wrote on last edited by
      #2

      There isn't a "bug" as such, the framework is written that way, but anyone would expect your code to be the syntax used . Instead, you should use this [less clear!] code:

      query.OrderBy(rec => rec.Name).ThenByDescending(rec => rec.Title);

      If you want to add another order by ascending just use .ThenBy instead.

      Sort of a cross between Lawrence of Arabia and Dilbert.[^]
      -Or-
      A Dead ringer for Kate Winslett[^]

      1 Reply Last reply
      0
      • D Dmitry Otblesk

        If I apply 2 OrderBy methods to my query, like that

        query.OrderBy(rec => rec.Name).OrderByDescending(rec => rec.Title);

        then only second method is applied, the first one is ignored. Is it a bug? What if I need to have ascending ordering for one column and descending ordering for another? Is it not achievable at all by method syntax?

        N Offline
        N Offline
        Not Active
        wrote on last edited by
        #3

        No its not a bug, its performing exactly as you have requested. Looking at the documentation you can see what is happening. OrderByDecending[^]: "Sorts the elements of a sequence in descending order according to a key." ThenByDescending[^]: "Performs a subsequent ordering of the elements in a sequence in descending order, according to a key." Notice the latter says a subsequent ordering.


        I know the language. I've read a book. - _Madmatt

        1 Reply Last reply
        0
        • D Dmitry Otblesk

          If I apply 2 OrderBy methods to my query, like that

          query.OrderBy(rec => rec.Name).OrderByDescending(rec => rec.Title);

          then only second method is applied, the first one is ignored. Is it a bug? What if I need to have ascending ordering for one column and descending ordering for another? Is it not achievable at all by method syntax?

          D Offline
          D Offline
          Dmitry Otblesk
          wrote on last edited by
          #4

          OK, thanks for explanation!

          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