QueryDesigner icon indicating copy to clipboard operation
QueryDesigner copied to clipboard

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

Open kerbybarrett opened this issue 5 years ago • 0 comments

Some IQueryable implementations don't also natively implement IOrderedQueryable<T> (DbSet<T>, 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!

kerbybarrett avatar Sep 10 '20 15:09 kerbybarrett