Sorting Linq Query Objects

8 posts, 0 answers
  1. Neo
    Neo avatar
    30 posts
    Member since:
    May 2009

    Posted 20 May 2009 Link to this post

    I have a Linq Query stored in session and bound to a gridview like this:
    Code:
    gvResults.DataSource = persons
    gvResults.DataBind()
    Session("PERSONS") = persons


    Each Time a user clicks a column header, I would like to sort the query. This would be the normal way I would do it if it was a dataTable stored in session:

    Code:
           
    Dim dt As DataTable = Session("PERSONS")
    Dim dv As DataView = New DataView(dt)
    dv.Sort = sortEx
    'Save back to Session so paging functionality remains
    Session("PERSONS") = dv.ToTable()
    gvResults.DataSource = Session("PERSONS")
    gvResults.DataBind()


    What is the alternative way to make this work with LINQ?
  2. PetarP
    Admin
    PetarP avatar
    754 posts

    Posted 21 May 2009 Link to this post

    Hello Michael,
    in the sorting event of the grid you can do a query similar to this one:
    IObjectScope scope = ObjectScopeProvider1.GetNewObjectScope(); 
    IQueryable<Offer> result = from c in scope.Extent<Offer>() 
                   .OrderBy(e.SortExpression.ToString()) 
                               select c;            
    GridView1.DataSource = result.ToList(); 
    GridView1.DataBind(); 
    Please do not hesitate to contact us if you face any further difficulties.

    Kind regards,
    PetarP
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Check out the tips for optimizing your support resource searches.
  3. DevCraft banner
  4. Jay
    Jay avatar
    28 posts
    Member since:
    Apr 2006

    Posted 15 Feb 2010 Link to this post

    I'm using Q3 2009 SP 1 and this syntax won't compile.  I can use a function parameter so that it looks like
    from a in Scope.Extent<T>().OrderBy(order => "Id") 
                                        select a; 
    but this produces the following runtime error.

    Unable to cast object of type 'Telerik.OpenAccess.Query.Context' to type 'OpenAccessRuntime.DataObjects.query.Node'.



    What's going on?  What am I doing wrong?
  5. Damyan Bogoev
    Admin
    Damyan Bogoev avatar
    581 posts

    Posted 17 Feb 2010 Link to this post

    Hello Jay,

    You should slightly modify the binding query in the following way:

    from a in Scope.Extent<T>().OrderBy(order => order.Id) select a;

    I think that will help you.

    All the best,
    Damyan Bogoev
    the Telerik team

    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
  6. Jay
    Jay avatar
    28 posts
    Member since:
    Apr 2006

    Posted 17 Feb 2010 Link to this post

    Unfortunately that doesn't allow me to do what I want to do -- which is to sort dynamically.
  7. Damyan Bogoev
    Admin
    Damyan Bogoev avatar
    581 posts

    Posted 19 Feb 2010 Link to this post

    Hello Jay,

    Unfortunately at the moment Telerik OpenAccess ORM does not support dynamic expressions inside an OrderBy clause. This functionality is on our to-do list and will be part of the product in the future.
    Your current option is to use the solution provided by Petar and pass the SortExpression to the OrderBy clause.

    Greetings,
    Damyan Bogoev
    the Telerik team

    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
  8. Jay
    Jay avatar
    28 posts
    Member since:
    Apr 2006

    Posted 13 Sep 2010 Link to this post

    Any update on the dynamic expressions inside the order by clause?  Is this featured getting any closer to being implemented?
  9. Damyan Bogoev
    Admin
    Damyan Bogoev avatar
    581 posts

    Posted 15 Sep 2010 Link to this post

    Hello Jay,

    You could try this code library example. It demonstrates how to use OpenAccess with Dynamic Linq. Additional useful information with regard to Dynamic Linq and OpenAccess can be found here.
    Hope that helps.

    Greetings,
    Damyan Bogoev
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
Back to Top
DevCraft banner