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

Extension.ApplySorting parameters are incorrect #23

Open
whighsmith opened this issue Jun 26, 2021 · 1 comment
Open

Extension.ApplySorting parameters are incorrect #23

whighsmith opened this issue Jun 26, 2021 · 1 comment
Assignees
Labels
bug Something isn't working

Comments

@whighsmith
Copy link

whighsmith commented Jun 26, 2021

    public static void ApplySorting(this IRootSpecification gridSpec,
        string sort,
        string orderByDescendingMethodName,
        string groupByMethodName)
    {

should be

    public static void ApplySorting(this IRootSpecification gridSpec,
        string sort,
        string orderByMethodName,
        string orderByDescendingMethodName)
    {

and very last "else" block should be...

            else
            {
                specificationType.GetMethod(
                        orderByMethodName,
                        BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic)
                    .Invoke(gridSpec, new object[] { propertyReturningExpression });
            }

awesome sample....thank you.

@whighsmith
Copy link
Author

whighsmith commented Jun 27, 2021

The following implementation of ListSpecificationBase supports OrderBy / ThenBy sorting

public abstract class ListSpecificationBase<T> : IListSpecification<T>
{
    public virtual List<Expression<Func<T, bool>>> Criterias { get; } = new();
    public List<Expression<Func<T, object>>> Includes { get; } = new();
    public List<string> IncludeStrings { get; } = new();
    public Expression<Func<T, object>> OrderBy { get; private set; }
    public Expression<Func<T, object>> ThenBy { get; private set; }
    public Expression<Func<T, object>> OrderByDescending { get; private set; }
    public Expression<Func<T, object>> ThenByDescending { get; private set; }
    public Expression<Func<T, object>> GroupBy { get; private set; }

    public int Take { get; private set; }
    public int Skip { get; private set; }
    public bool IsPagingEnabled { get; set; }

    protected void ApplyIncludeList(IEnumerable<Expression<Func<T, object>>> includes)
    {
        foreach (var include in includes)
        {
            AddInclude(include);
        }
    }

    protected void AddInclude(Expression<Func<T, object>> includeExpression)
    {
        Includes.Add(includeExpression);
    }

    protected void ApplyIncludeList(IEnumerable<string> includes)
    {
        foreach (var include in includes)
        {
            AddInclude(include);
        }
    }

    protected void AddInclude(string includeString)
    {
        IncludeStrings.Add(includeString);
    }

    protected IListSpecification<T> ApplyFilterList(IEnumerable<FilterModel> filters)
    {
        foreach (var (fieldName, comparision, fieldValue) in filters)
        {
            ApplyFilter(PredicateBuilder.Build<T>(fieldName, comparision, fieldValue));
        }

        return this;
    }

    protected IListSpecification<T> ApplyFilter(Expression<Func<T, bool>> expr)
    {
        Criterias.Add(expr);

        return this;
    }

    protected void ApplyPaging(int skip, int take)
    {
        Skip = skip;
        Take = take;
        IsPagingEnabled = true;
    }

    protected void ApplyOrderBy(Expression<Func<T, object>> orderByExpression) =>
        OrderBy = orderByExpression;

    protected void ApplyThenOrderBy(Expression<Func<T, object>> thenByExpression) =>
        ThenBy = thenByExpression;

    protected void ApplyOrderByDescending(Expression<Func<T, object>> orderByDescendingExpression) =>
        OrderByDescending = orderByDescendingExpression;

    protected void ApplyThenOrderByDescending(Expression<Func<T, object>> thenByDescendingExpression) =>
        ThenByDescending = thenByDescendingExpression;

    protected void ApplyGroupBy(Expression<Func<T, object>> groupByExpression) =>
        GroupBy = groupByExpression;

    protected void ApplySortingList(IList<string> sorts)
    {
        if (sorts.Count > 0)
        {
            //  Get first sort item and remove it from sorts list
            var sort1 = sorts[0];
            sorts.RemoveAt(0);

            //  Apply OrderBy to first item
            ApplySorting(sort1);

            //  Apply ThenBy to subsequent items
            foreach (var sort in sorts)
            {
                ApplyThenBySorting(sort);
            }
        }
    }

    protected void ApplySorting(string sort)
    {
        this.ApplySorting(sort, nameof(ApplyOrderBy), nameof(ApplyOrderByDescending));
    }

    protected void ApplyThenBySorting(string sort)
    {
        this.ApplySorting(sort, nameof(ApplyThenOrderBy), nameof(ApplyThenOrderByDescending));
    }
}

@thangchung thangchung self-assigned this Jul 29, 2021
@thangchung thangchung added the bug Something isn't working label Jul 29, 2021
thangchung added a commit that referenced this issue Jul 29, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants