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:
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:
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