Using LINQ to dynamic sort data in GridPanel

  1. #1

    Using LINQ to dynamic sort data in GridPanel

    Hi, everybody,
    In the source code of, you can find a function names "GetCustomers" in DataController.cs. By it's signature
    public AjaxStoreResult GetCustomers(int limit, int start, string dir, string sort)
    we can know it ueses dir and sort to sort data.
    But reading code, it uses LINQ to implement search and sort. We kown, LINQ doesn't support order by a static string. So this line
    orderby(string.Concat(sort, " ", dir))
    was not working in fact.

    I think below is a better method to dynamic sort data using LINQ. (by
    Dim selector = GetType(Customer).GetProperty(sort)
    if String.Compare("ASC", dir, true) then
        query = query.OrderBy(Function(c) selector.GetValue(c, nothing))
        query = query.OrderByDescending(Function(c) selector.GetValue(c, nothing))
    end if
    Maybe it needs refactor to better smell, but it's now working!
  2. #2

    RE: Using LINQ to dynamic sort data in GridPanel

    The .OrderBy Method is an Extension Method which enable passing a string query. See System.Linq.Dynamic.DynamicQueryable

    Breaking out .OrderBy into the if/else block is fine, but just not a clean in my opinion.

    Hope this helps.

    Geoffrey McGill
  3. #3

    RE: Using LINQ to dynamic sort data in GridPanel

    Aha, I didn't notice this namespace. It's a more clever method than me.
    Why did I not find this namespace from msdn? It's a new namespace under System.Linq? I found a Microsoft sign on the head of the code file.
  4. #4

    RE: Using LINQ to dynamic sort data in GridPanel

    Guys does any one can help me out in my scenario I am using in my project simple query and fetching records and populating a dataset and then I want to use it for gridpanel but with paging sorting functionallity, i also check out the paging and sorting example given but it uses LINQ and list with NortwindDataContext way to achieve this functions. I want to achieve it by dataset as it is already implemented in my project and i think it will give us new way of implementation of paging and sorting using coolite gridpanel, your quick response is awaited

Similar Threads

  1. [CLOSED] How to bind linq to sql result to data gridpanel
    By Daly_AF in forum 1.x Legacy Premium Help
    Replies: 1
    Last Post: May 10, 2012, 11:22 AM
  2. Store Data is not displaying in Dynamic Gridpanel
    By NishaLijo in forum 1.x Help
    Replies: 1
    Last Post: Apr 11, 2012, 9:18 PM
  3. Replies: 4
    Last Post: Jul 25, 2011, 4:57 PM
  4. Replies: 0
    Last Post: Feb 10, 2011, 1:04 PM
  5. Data prepare doesn't work with Linq to Entities
    By flaviodamaia in forum 1.x Help
    Replies: 4
    Last Post: Nov 17, 2009, 12:45 PM

Posting Permissions