Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DataGrid sorting support ef with DataGridExtensions.EFOrderBySortDefinitionsing #6682

Open
neozhu opened this issue Apr 18, 2023 · 0 comments · May be fixed by #8254
Open

DataGrid sorting support ef with DataGridExtensions.EFOrderBySortDefinitionsing #6682

neozhu opened this issue Apr 18, 2023 · 0 comments · May be fixed by #8254
Labels
enhancement New feature or request

Comments

@neozhu
Copy link
Contributor

neozhu commented Apr 18, 2023

Feature request type

Enhance component

Component name

MudDataGrid

Describe the solution you'd like

T is EF Entity class, T1 is Dto class for datagrid binding source type.

public static class DataGridExtensions
{
    public static IQueryable<T> EFOrderBySortDefinitions<T,T1>(this IQueryable<T> source, GridState<T1> state)
        => EFOrderBySortDefinitions(source, state.SortDefinitions);

    public static IQueryable<T> EFOrderBySortDefinitions<T,T1>(this IQueryable<T> source, ICollection<SortDefinition<T1>> sortDefinitions)
    {
        // avoid multiple enumeration
        var sourceQuery = source;

        if (sortDefinitions.Count == 0)
            return sourceQuery;

        IOrderedQueryable<T>? orderedQuery = null;

        foreach (var sortDefinition in sortDefinitions)
        {
            var parameter = Expression.Parameter(typeof(T), "x");
            var orderByProperty = Expression.Property(parameter, sortDefinition.SortBy);
            var sortlambda= Expression.Lambda(orderByProperty, parameter);
            if (orderedQuery is null)
            {
                var sortmethod = typeof(Queryable).GetMethods()
                .Where(m => m.Name == (sortDefinition.Descending? "OrderByDescending" : "OrderBy") && m.IsGenericMethodDefinition)
                .Where(m => m.GetParameters().ToList().Count == 2) // ensure selecting the right overload
                .Single();
                var genericMethod = sortmethod.MakeGenericMethod(typeof(T), orderByProperty.Type);
                orderedQuery = (IOrderedQueryable<T>?)genericMethod.Invoke(genericMethod, new object[] { source, sortlambda });
            }
            else
            {
                var sortmethod = typeof(Queryable).GetMethods()
                .Where(m => m.Name == (sortDefinition.Descending ? "ThenByDescending" : "ThenBy") && m.IsGenericMethodDefinition)
                .Where(m => m.GetParameters().ToList().Count == 2) // ensure selecting the right overload
                .Single();
                var genericMethod = sortmethod.MakeGenericMethod(typeof(T), orderByProperty.Type);
                orderedQuery = (IOrderedQueryable<T>?)genericMethod.Invoke(genericMethod, new object[] { source, sortlambda });
            }
        }
        return orderedQuery ?? sourceQuery;
    }


}
@neozhu neozhu added enhancement New feature or request triage labels Apr 18, 2023
@neozhu neozhu changed the title DataGrid sort support ef with DataGridExtensions.EFOrderBySortDefinitions DataGrid sorting support ef with DataGridExtensions.EFOrderBySortDefinitionsing Apr 18, 2023
0xced added a commit to 0xced/MudBlazor that referenced this issue Feb 27, 2024
0xced added a commit to 0xced/MudBlazor that referenced this issue Feb 27, 2024
0xced added a commit to 0xced/MudBlazor that referenced this issue Feb 28, 2024
0xced added a commit to 0xced/MudBlazor that referenced this issue Mar 14, 2024
0xced added a commit to 0xced/MudBlazor that referenced this issue Mar 15, 2024
@henon henon removed the triage label May 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants