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 SQL Ascending or Descending sort depening on boolean

LINQ to SQL Ascending or Descending sort depening on boolean

Scheduled Pinned Locked Moved LINQ
databasequestioncsharpsql-serverlinq
3 Posts 3 Posters 3 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.
  • E Offline
    E Offline
    Eduard Keilholz
    wrote on last edited by
    #1

    I have a function which returns messages from my datasource, however I want to pimp that function creating two parameters. One is the field to sort the result on, the other is wether to sort ascending or descending. My LINQ query looks like this : var qMessage = from dtMessages in dcMessaging.t_Messages orderby OrderBy ascending select dtMessages; The OrderBy variable is a string which contains the fieldname to sort on. Then ascending here obviously says the result should be sorted ascending. I want to be able to sort ascending or descending depending on a boolean called lets say bSortAscending. When the boolean is true, sort Ascending, else descending. And now, my secret question number two... Ofcourse I need to send a fieldname to the method to sort the result on. Calling the function (called RetrieveMessages) would look like this : RetrieveMessages("FieldName", true); Is there a way I can retrieve fieldnames from my datasource (SQL Server) from the datacontext? If I change the field in the datasource to FieldName2 I need to create a new datacontext and the compiler nags me everywhere where FieldName is used, however the call to RetrieveMessages("FieldName", true) is still allowed since FieldName is just a string. I want to call the function in kind of the following way : RetrieveMessage(MessagesDataContext.t_Messages.FieldName.Name, true); This way the compiler will remind me to update the function call when I created a new datacontext and the field FieldName doesn't exist anymore...

    .: I love it when a plan comes together :. http://www.zonderpunt.nl

    J J 2 Replies Last reply
    0
    • E Eduard Keilholz

      I have a function which returns messages from my datasource, however I want to pimp that function creating two parameters. One is the field to sort the result on, the other is wether to sort ascending or descending. My LINQ query looks like this : var qMessage = from dtMessages in dcMessaging.t_Messages orderby OrderBy ascending select dtMessages; The OrderBy variable is a string which contains the fieldname to sort on. Then ascending here obviously says the result should be sorted ascending. I want to be able to sort ascending or descending depending on a boolean called lets say bSortAscending. When the boolean is true, sort Ascending, else descending. And now, my secret question number two... Ofcourse I need to send a fieldname to the method to sort the result on. Calling the function (called RetrieveMessages) would look like this : RetrieveMessages("FieldName", true); Is there a way I can retrieve fieldnames from my datasource (SQL Server) from the datacontext? If I change the field in the datasource to FieldName2 I need to create a new datacontext and the compiler nags me everywhere where FieldName is used, however the call to RetrieveMessages("FieldName", true) is still allowed since FieldName is just a string. I want to call the function in kind of the following way : RetrieveMessage(MessagesDataContext.t_Messages.FieldName.Name, true); This way the compiler will remind me to update the function call when I created a new datacontext and the field FieldName doesn't exist anymore...

      .: I love it when a plan comes together :. http://www.zonderpunt.nl

      J Offline
      J Offline
      JimBob SquarePants
      wrote on last edited by
      #2

      Hi Eduard, Have a look at this addon for Visualstudio 2008. Not only will it allow you to create your linq queries diagrammatically , it also exposes the code so that you can learn the correct constructor logic. http://blogs.msdn.com/mitsu/archive/2008/04/02/visual-linq-query-builder-for-linq-to-sql-vlinq.aspx[^] Good luck Jimbob

      1 Reply Last reply
      0
      • E Eduard Keilholz

        I have a function which returns messages from my datasource, however I want to pimp that function creating two parameters. One is the field to sort the result on, the other is wether to sort ascending or descending. My LINQ query looks like this : var qMessage = from dtMessages in dcMessaging.t_Messages orderby OrderBy ascending select dtMessages; The OrderBy variable is a string which contains the fieldname to sort on. Then ascending here obviously says the result should be sorted ascending. I want to be able to sort ascending or descending depending on a boolean called lets say bSortAscending. When the boolean is true, sort Ascending, else descending. And now, my secret question number two... Ofcourse I need to send a fieldname to the method to sort the result on. Calling the function (called RetrieveMessages) would look like this : RetrieveMessages("FieldName", true); Is there a way I can retrieve fieldnames from my datasource (SQL Server) from the datacontext? If I change the field in the datasource to FieldName2 I need to create a new datacontext and the compiler nags me everywhere where FieldName is used, however the call to RetrieveMessages("FieldName", true) is still allowed since FieldName is just a string. I want to call the function in kind of the following way : RetrieveMessage(MessagesDataContext.t_Messages.FieldName.Name, true); This way the compiler will remind me to update the function call when I created a new datacontext and the field FieldName doesn't exist anymore...

        .: I love it when a plan comes together :. http://www.zonderpunt.nl

        J Offline
        J Offline
        Judah Gabriel Himango
        wrote on last edited by
        #3

        Eduard Keilholz wrote:

        I want to be able to sort ascending or descending depending on a boolean called lets say bSortAscending. When the boolean is true, sort Ascending, else descending.

        Remove the whole orderby clause from your LINQ query. Then do

        var ordered = bSortAscending ? qMessage.OrderBy(...) : qMessage.OrderByDescending(...);

        Eduard Keilholz wrote:

        Is there a way I can retrieve fieldnames from my datasource (SQL Server) from the datacontext?

        That I don't know. Perhaps someone else can help you with this.

        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