ExpressionBuilder But For Sorting

1 Answer 32 Views
Grid
Mike
Top achievements
Rank 1
Iron
Mike asked on 29 Dec 2023, 06:44 PM | edited on 29 Dec 2023, 06:45 PM

My solution layout follows this:

  • Data - EF entities and DB Context
  • Services - Service objects that interact with the database (references the Data project directly)
  • Web - Front end (controllers and views) that use the Services project to push/pull data and reflect it back to the user

I recently discovered the ExpressionBuilder to handle grid filtering and convert it to LINQ to use in my Service objects to (efficiently) query the database (pushes the filter down to the DB query) so only a limited number of records are returned (thus, loaded into memory) instead of loading all records and then filtering them in memory.

My question, is there a similar object for Sorting functionality?  So it can convert my Grid Sort (from the DataSourceRequest object) into a LINQ OrderBy expression and apply it against my entity type so it can be passed into the DB query?  Or am I stuck writing my own?

1 Answer, 1 is accepted

Sort by
1
Accepted
Mihaela
Telerik team
answered on 03 Jan 2024, 02:00 PM

Hi Mike,

Indeed, you can create Sort expressions via the ExpresionBuilder. For instance, you could make use of the built-in logic for sorting the data source. Here is an extract of the source code that creates the query dynamically:

        public IQueryable Sort()
        {
            IQueryable query = queryable;
            bool isFirst = true;

            foreach (var descriptor in this.sortDescriptors)
            {
                Type memberType = typeof(object);
                var descriptorBuilder = ExpressionBuilderFactory.MemberAccess(queryable, memberType, descriptor.Member);
                var expression = descriptorBuilder.CreateLambdaExpression();

                string methodName = "";
                if (isFirst)
                {
                    methodName = descriptor.SortDirection == ListSortDirection.Ascending ?
                        "OrderBy" : "OrderByDescending";
                    isFirst = false;
                }
                else
                {
                    methodName = descriptor.SortDirection == ListSortDirection.Ascending ?
                        "ThenBy" : "ThenByDescending"; 
                }

                query = query.Provider.CreateQuery(
                    Expression.Call(
                        typeof(Queryable),
                        methodName,
                        new[] { query.ElementType, expression.Body.Type },
                        query.Expression,
                        Expression.Quote(expression)));

            }
            return query;
        }

The SortDescriptors are built based on the values sent from the client-side:

        public SortDescriptor(string member, ListSortDirection order)
        {
            Member = member;
            SortDirection = order;
        }

Also, you can review an example of custom sorting in the Grid Custom AJAX Binding online demo ("View Source" section --> CustomAjaxBindingController.cs --> ApplyOrdersSorting() method).

I hope that helps.


Regards,
Mihaela
Progress Telerik

Stay tuned by visiting our public roadmap and feedback portal pages. If you're new to the Telerik family, be sure to check out our getting started resources, as well as the only REPL playground for creating, saving, running, and sharing server-side code.
Tags
Grid
Asked by
Mike
Top achievements
Rank 1
Iron
Answers by
Mihaela
Telerik team
Share this question
or