question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Example documentation - best practices to extract column for server side sorting?

See original GitHub issue

Hello, fantastic library, just had a question on best practices in sorting via LINQ based on the IDataTablesRequest request

For example, I know I can extract the given column for which the sort has been applied on the client side by:

var sortColumn = request.Columns.Where(c => c.Sort != null).FirstOrDefault();

From there, I have access to the field name as a string and can check the string to conditionally create a where clause, but is there a better way?

Issue Analytics

  • State:open
  • Created 8 years ago
  • Comments:10 (1 by maintainers)

github_iconTop GitHub Comments

26reactions
sintetico82commented, Dec 18, 2016

Hi! inspired by this stackoverflow answer: http://stackoverflow.com/a/36303246/726868

I create this extension method for IQueryable OrderBy

 public static IQueryable<T> OrderBy<T>(this IQueryable<T> source, IEnumerable<DataTables.AspNet.Core.IColumn> sortModels)
        {
            var expression = source.Expression;
            int count = 0;
            foreach (var item in sortModels)
            {
                var parameter = Expression.Parameter(typeof(T), "x");
                var selector = Expression.PropertyOrField(parameter, item.Field);
                var method = item.Sort.Direction == DataTables.AspNet.Core.SortDirection.Descending ?
                    (count == 0 ? "OrderByDescending" : "ThenByDescending") :
                    (count == 0 ? "OrderBy" : "ThenBy");
                expression = Expression.Call(typeof(Queryable), method,
                    new Type[] { source.ElementType, selector.Type },
                    expression, Expression.Quote(Expression.Lambda(selector, parameter)));
                count++;
            }
            return count > 0 ? source.Provider.CreateQuery<T>(expression) : source;
        }

So you can active your goals like this:

 var orderColums = request.Columns.Where(x => x.Sort != null);
 var dataPage = data.OrderBy(orderColums).Skip(request.Start).Take(request.Length);

bye

4reactions
sintetico82commented, Sep 6, 2018

It’s so funny. I was searching for the same solution now… and i found the answer of myself ahaha We live in the loop!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Server-Side and Client-Side Sorting and Filtering
If there are not many records in a table, they are all shown in the result grid after retrieving data from a table...
Read more >
REST API Design: Filtering, Sorting, and Pagination
However, there is no standard or official API design guidelines. ... May require more work on server side to parse and group the...
Read more >
Tutorial: Add sorting, filtering, and paging with the Entity ...
Add column sort links. To add sorting to the Student Index page, you'll change the Index method of the Student controller and add...
Read more >
Server-Side Paging and Sorting for a Microflow Data Source
Double-click the Create object activity. Click New to set the value for another member of the Paging entity. Select Member > IsSortable (Boolean) ......
Read more >
Angular Material Data Table: A Complete Example
A complete example of an Angular Material Data Table with server-side pagination, sorting, filtering, as well as a loading indicator.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found