Why can't I call Skip to IOrderedQueryable?

I return IOrderedQueryable<T>like this:

response.Data = Expressions
    .ApplySorts(request, result)
    .Skip(request.Skip)
    .Take(request.Take)
    .ToList();

Here is my class that contains a method for returning IOrderedQueryable<T>.

public static class Expressions
{
    public static IOrderedQueryable<T> ApplySorts<T>(
        DataRequest request, IQueryable<T> items)
        {
            var sorts = request.Sort;

            if (sorts != null)
            {
                foreach (var sort in sorts)
                {
                    items = items.OrderBy(sort.Field + " " + sort.Dir);
                }
            }

            return (IOrderedQueryable<T>)items;
        }
}

But at runtime, I get this error:

{"The Skip method is supported only for sorted input in LINQ for Entity. The OrderBy method must be called before the Skip method." }

Please note that there are currently no species; however, the return value ApplySorts<T>()should still return the type that expects Skip()no?

+4
source share
2 answers

. TOP, LIMIT BETWEEN , ( Take, ).

: undefined ( , ). , ORM (.. Skip ).

: Skip(10) Order, . , 10 , , . , 10 , 5, , 5 ? 5 - .

, . .

+8

T , SQL :

public static IOrderedQueryable<T> ApplySorts<T>(DataRequest request, IQueryable<T> items)
{
    var sorts = request.Sort;

    var keyProperty = typeof(T).GetProperties().FirstOrDefault(prop => 
        prop.IsDefined(typeof(System.ComponentModel.DataAnnotations.KeyAttribute), false));

    if (sorts != null)
    {
        foreach (var sort in sorts)
        {
            items = items.OrderBy(sort.Field + " " + sort.Dir);
        }
    }
    else
    {
        items = items.OrderBy(keyProperty.Name); // set a default sort if there are no sorts provided
    }

    return (IOrderedQueryable<T>)items;
}
0

Source: https://habr.com/ru/post/1625545/


All Articles