-
Notifications
You must be signed in to change notification settings - Fork 134
Example documentation - best practices to extract column for server side sorting? #26
Comments
Hello, @TimeBomb006. First of all, thanks for your feedback. I'm sorry to inform that there isn't a better way to do that. This is a limitation imposed by how DataTables request are sent and to avoid extremely complex code for this library. You can also refer to the |
Hi! I create this extension method for IQueryable OrderBy
So you can active your goals like this:
bye |
sintetico82 - Big UP for the reference code. Works like a charm. |
Some fixes and extra extensions: using DataTables.AspNet.Core;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
namespace MyApp.Extensions
{
public static class DataTablesExtensions
{
public static IQueryable<T> SortAndPage<T>(this IQueryable<T> source, IDataTablesRequest request)
{
return source.OrderBy(request.Columns).Page(request);
}
public static IQueryable<T> Page<T>(this IQueryable<T> source, IDataTablesRequest request)
{
return source.Skip(request.Start).Take(request.Length);
}
public static IQueryable<T> OrderBy<T>(this IQueryable<T> source, IEnumerable<IColumn> sortModels)
{
var expression = source.Expression;
var count = 0;
foreach (var item in sortModels.Where(x=>x.Sort != null).OrderBy(x => x.Sort.Order))
{
var parameter = Expression.Parameter(typeof(T), "x");
var selector = Expression.PropertyOrField(parameter, item.Field);
var method = item.Sort.Direction == SortDirection.Descending ?
(count == 0 ? nameof(Queryable.OrderByDescending) : nameof(Queryable.ThenByDescending)) :
(count == 0 ? nameof(Queryable.OrderBy) : nameof(Queryable.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;
}
}
} |
It's so funny. I was searching for the same solution now... and i found the answer of myself ahaha |
I realize this thread is old but I will ask anyway. If i have an enumerable can I use this? If not, when would I have a queryable? If I can use this with enumerable is there equivalent code for filtering? I tried converting the enumerable to queryable using asqueryable() but the code copied from above throws exception with nulls or something on columns. So is it a bug with the code above or converting enumerable to queryable. Thanks in advance |
@professordave I think you should share your exceptions. I would think using the |
Thanks I will do on Monday. Then the follow up, is there an equivalent code for the search? In what scenario would you need/ use a sort but not a search? |
Sort and Page works!! Thanks! Now what about filter/search? We are able to use sort and page against an in memory object, enumerable, works. What about filtering? Thanks in advance |
Lines 66 to 68 in 115ee9a
|
Hello, fantastic library, just had a question on best practices in sorting via LINQ based on the
IDataTablesRequest
requestFor example, I know I can extract the given column for which the sort has been applied on the client side by:
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?
The text was updated successfully, but these errors were encountered: