Sunday, 15 March 2009

Using LINQ Dynamic Query Library with the ObjectDataSource

Recently I stumbled across the Dynamic Query Library for LINQ, which comes as part of the Visual C# 2008 examples. Scott Gu has blogged about using the Dynamic Query Library with LINQ to SQL.

In this post, I show an example of using it with the ObjectDataSource Web Server Control in ASP.NET.

The requirement is to display a grid in ASP.NET that allows sorting via the middle tier. Here is an example of the GUI (clicking on the headers sorts the data).


For our example, we will bind to the following business object (Made as simple as possible, using automatic properties):

public class Person


        public int Age { get; set; }

        public string FirstName { get; set; }

        public string LastName { get; set; }


We then need to write a class to to get the data and apply the sorting (in a proper application this might be done across two tiers). To make the code as short as possible, I have used C# 3.0 Object Initialisers and Collection Initialisers.

public static class FakeDb


        public static List<Person> GetPeople(string sortExpression)


            List<Person> persons = new List<Person>(3)


                new Person { Age = 10, FirstName = "Richard", LastName = "Smith" },

                new Person { Age = 34, FirstName = "Bob", LastName = "Jones" },

                new Person { Age = 50, FirstName = "Lisa", LastName = "Jones" }



            if (!string.IsNullOrEmpty(sortExpression))


                persons = persons.AsQueryable().OrderBy(sortExpression).ToList();



            return persons;



The line to note is the line using the dynamic query extension methods:

people = people.AsQueryable().OrderBy(sortExpression).ToList();

To allow the List to use this extension method, we first need to convert the List to IQueryable, which is achieved using the AsQueryable extension method. We then apply the Dynamic Query Extension OrderBy, which takes the name of the property to sort on and the sort direction- for example “Age DESC”. We then convert the IQueryable result back to a List and return it.

We then just need the following in our ASPX (formatting attributes have been removed):

<asp:GridView ID="grdViewPersons" runat="server" AllowSorting="True"
            AutoGenerateColumns="False" DataSourceID="objSrcFakeData" >
                <asp:BoundField DataField="Age" HeaderText="Age" SortExpression="Age" />
                <asp:BoundField DataField="FirstName" HeaderText="FirstName"
                    SortExpression="FirstName" />
                <asp:BoundField DataField="LastName" HeaderText="LastName"
                    SortExpression="LastName" />
        <asp:ObjectDataSource ID="objSrcFakeData" runat="server"
            SelectMethod="GetPeople" TypeName="DynamicQuerySorting.FakeDb" SortParameterName="sortExpression">

This code makes sorting a breeze! You can also use Dynamic Query sorting if you are using databinding, by hooking into the GridView.Sorting Event.

No comments:

Post a Comment