Skip to content
This repository was archived by the owner on Dec 2, 2020. It is now read-only.
This repository was archived by the owner on Dec 2, 2020. It is now read-only.

OrderBy<T>(this IQueryable<T> query, IEnumerable<OrderFilter> filters) fails with cast exception #25

Description

@kerbybarrett

Some IQueryable implementations don't also natively implement IOrderedQueryable (DbSet, for example), so the initial cast in this method fails.

            var res = (IOrderedQueryable<T>)query;
            var step = OrderStep.First;
            if (filters != null)
                foreach (var filter in filters)
                {
                    res = filter.GetOrderedQueryable(res, step);
                    step = OrderStep.Next;
                }
            return res;

Instead of trying to cast at the beginning, use a conditional to pass the correct instance on each iteration:

            IOrderedQueryable<T> res = null;
            var step = OrderStep.First;
            if (filters != null)
                foreach (var filter in filters)
                {
                    res = filter.GetOrderedQueryable(step == OrderStep.First ? query : res, step);
                    step = OrderStep.Next;
                }
            return res;

I would submit a PR, but I'm at work on my personal GitHub account and it's too cumbersome to push my commit, so providing this fix here in the hopes that it can be fixed by the dev. Thank you!

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions