This is a migrated thread and some comments may be shown as answers.

Sorting Linq Query Objects

7 Answers 186 Views
LINQ (LINQ specific questions)
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Neo
Top achievements
Rank 1
Neo asked on 20 May 2009, 02:03 PM
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?

7 Answers, 1 is accepted

Sort by
0
PetarP
Telerik team
answered on 21 May 2009, 08:20 AM
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.
0
Jay
Top achievements
Rank 1
answered on 15 Feb 2010, 05:20 PM
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?
0
Damyan Bogoev
Telerik team
answered on 17 Feb 2010, 05:39 PM
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.
0
Jay
Top achievements
Rank 1
answered on 17 Feb 2010, 10:37 PM
Unfortunately that doesn't allow me to do what I want to do -- which is to sort dynamically.
0
Damyan Bogoev
Telerik team
answered on 19 Feb 2010, 07:39 PM
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.
0
Jay
Top achievements
Rank 1
answered on 13 Sep 2010, 10:19 PM
Any update on the dynamic expressions inside the order by clause?  Is this featured getting any closer to being implemented?
0
Damyan Bogoev
Telerik team
answered on 15 Sep 2010, 05:04 PM
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
Tags
LINQ (LINQ specific questions)
Asked by
Neo
Top achievements
Rank 1
Answers by
PetarP
Telerik team
Jay
Top achievements
Rank 1
Damyan Bogoev
Telerik team
Share this question
or