LINQ, “orderby” and dynamic query sample

Damir Dobric Posts

Next talks:

 

    

Follow me on Twitter: #ddobric



 

 

Archives

One of important benefits of LINQ is that it enables you to write type-safe queries in VB and C#. Unfortunately there are Business cases cases where you want the flexibility to dynamically construct queries on the fly and damage type safety of Linq. :)

Here is one example:

image

Now, what if you use this code to bind the data to some grid, and user have a possibility to change the sorting key?
Following example shows a little trick which self-explains how to do it:

image
Note: The variable 'sortByKey' could be dynamically entered by user.

Another, more general solution related to dynamic querying can be obtained here: http://msdn.microsoft.com/en-us/vcsharp/bb894665.aspx.
The link contains a Dynamic Query Library provided by the LINQ team to dynamically construct LINQ queries. More about this, can be found
in Scott's blog.

Last but not least, another interesting, but a bit more complicated and for Linq less natural way to do it can be found here.


Posted Jun 14 2008, 11:22 PM by Damir Dobric
Filed under:

Comments

Bernhard Hofmann wrote re: LINQ, “orderby” and dynamic query sample
on 03-20-2009 8:35

Thank you. This is exactly what I was looking for. I'm using an enum for the sort order, so I need to convert that, and your orderBy delegate is just the thing I needed.

Keith Hooker wrote re: LINQ, “orderby” and dynamic query sample
on 08-21-2009 16:04

Thanks Damir, this is great!  

I have a situation where I may have EITHER one or two columns to be returned.   If your orderBy function returns a list of objects, is LINQ smart enough to use that in the OrderBy clause?

Leonardo Fucci wrote re: LINQ, “orderby” and dynamic query sample
on 08-24-2009 16:22

I cant Believe it... I was exceptical, i didnt believe that this could work, but IT DOES!!!

THANKS A LOT!!!

I give you here the source for this example in VB.Net, its what i work.

   Protected Sub gridRRRR_Sorting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewSortEventArgs) Handles gridReporte.Sorting

       Dim linQuery = From C In xReporteObtenido Order By OORRBY(e.SortExpression, C) Select C

   Private Function OORRBY(ByVal SortKey As String, ByVal x As capaR_bzzReporte.ReporteCapturas)

       Select Case SortKey

           Case "xxx"

               Return x.xxx

           Case "yyy"

               Return x.yyy

           Case "zzz"

               Return x.zzz

           Case "www"

               Return x.www

       End Select

   End Function

Markus Schulze wrote re: LINQ, “orderby” and dynamic query sample
on 10-15-2009 17:01

Oh, Thank you. I looked for my sorting solution a long time.

Thanx, it works!

Greetings from Germany.

Markus

Will wrote re: LINQ, “orderby” and dynamic query sample
on 04-15-2010 21:39

Perfect solution...just what I was looking for

ile wrote re: LINQ, “orderby” and dynamic query sample
on 06-02-2010 12:14

How to dynamically sort "descending" (not ascending)?

Thanks!

Ilija

Krishantha wrote re: LINQ, “orderby” and dynamic query sample
on 06-07-2010 20:41

orderby Orderby(sortKey, city) decending

Danny wrote re: LINQ, “orderby” and dynamic query sample
on 06-29-2010 13:26

I get the error cannot orderby by type object

Chris Kimpton wrote re: LINQ, “orderby” and dynamic query sample
on 07-05-2010 11:48

Hi,

Thanks for this.

Like @ile, I'd like to also dynamically, ie user selected, choose between asc/desc.

I wonder if the "descending" keyword could come from a function...

Thanks, Chris

Chris Kimpton wrote re: LINQ, “orderby” and dynamic query sample
on 07-05-2010 13:46

Hi,

RE: dynamically choosing ascending/descending, I found this post:

weblogs.asp.net/.../dynamic-sorting-with-linq.aspx

Regards, Chris

Lasse wrote re: LINQ, “orderby” and dynamic query sample
on 11-24-2010 10:27

I tried the solution, but got an exception :

LINQ to Entities does not recognize the method 'System.Object TestOrderBy(String, OrderVO)' method, and this method cannot be translated into a store expression.

How come ? Somebody know or had an expirence with this problem ?

Best regards

Lasse

Lasse wrote re: LINQ, “orderby” and dynamic query sample
on 11-24-2010 10:27

I tried the solution, but got an exception :

LINQ to Entities does not recognize the method 'System.Object TestOrderBy(String, OrderVO)' method, and this method cannot be translated into a store expression.

How come ? Somebody know or had an expirence with this problem ?

Best regards

Lasse

Damir Dobric wrote re: LINQ, “orderby” and dynamic query sample
on 11-26-2010 22:56

LINQ to Entities does not recognize the method in general! The example I have shown above works with LINQ to objects only.

Rathin Pandya wrote re: LINQ, “orderby” and dynamic query sample
on 12-23-2010 16:43

very good example

developers.de wrote re: LINQ, “orderby” and dynamic query sample
on 04-22-2011 10:16

Linq orderby and dynamic query sample.. Smashing :)

UD wrote re: LINQ, “orderby” and dynamic query sample
on 01-31-2012 0:04

Greatest article on this topic out of the many I came across. Simple,  nice and clean. Saved my time a lot. Keep up the good work. Thanks a lot

Marc wrote re: LINQ, “orderby” and dynamic query sample
on 02-09-2012 15:40

Thanx for the good example! It helped me ordering some columns

Alain wrote re: LINQ, “orderby” and dynamic query sample
on 04-05-2012 16:16

Just try DataBinder.Eval

Example :

pListSource = (From aa In pListSource Order By DataBinder.Eval(aa, varName)).ToList

Just replace varName by your variable name and you will be able to sort by name !

dragan wrote re: LINQ, “orderby” and dynamic query sample
on 10-16-2012 2:38

Svaka cast majstore - ubih se trazeci resenje

developers.de is a .Net Community Blog powered by daenet GmbH.