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