locked
linq skip and take error RRS feed

  • Question

  • User-974983964 posted
    using (testContext context = new testContext())
    {
        var data = context.People.AsQueryable();
        //Sort
        if (sort.Equals("Created", StringComparison.OrdinalIgnoreCase))
        {
            if (order.Equals("desc", StringComparison.OrdinalIgnoreCase))
            {
                data = data.OrderByDescending(d => d.Created);
            }
            else
            {
                data = data.OrderBy(d => d.Created);
            }
        }
        //TakePage
        data = data.Skip((page - 1) * pagesize).Take(pagesize);
        //Console
        foreach (var item in data)
        {
            Console.WriteLine("{0},{1},{2}", item.Id, item.Name, item.Created);
        }
    }

    data = data.Skip((page - 1) * pagesize).Take(pagesize);

    this is error:The method 'Skip' is only supported for sorted input in LINQ to Entities

    Friday, December 25, 2015 8:28 AM

Answers

  • User1724605321 posted

    Hi Melody ,

    According to your code and the error message , if this condition not match :

      if (sort.Equals("Created", StringComparison.OrdinalIgnoreCase))
    

    The linq will not be sorted , it will cause the error . You could debug your application to confirm that .

    Best Regards,

    Nan Yu

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, December 25, 2015 8:50 AM

All replies

  • User1724605321 posted

    Hi Melody ,

    According to your code and the error message , if this condition not match :

      if (sort.Equals("Created", StringComparison.OrdinalIgnoreCase))
    

    The linq will not be sorted , it will cause the error . You could debug your application to confirm that .

    Best Regards,

    Nan Yu

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, December 25, 2015 8:50 AM
  • User-974983964 posted

    The linq will not be sorted , it will cause the error . You could debug your application to confirm that .

    Best Regards,

    thank u .Then I modified like this ,it is over.

    using (testContext context = new testContext())
    {
        var data = context.People.AsQueryable();
        //Sort
        if (sort.Equals("Created", StringComparison.OrdinalIgnoreCase))
        {
            if (order.Equals("desc", StringComparison.OrdinalIgnoreCase))
            {
                data = data.OrderByDescending(d => d.Created);
            }
            else
            {
                data = data.OrderBy(d => d.Created);
            }
        }
        else
        {
            data = data.OrderBy(d => d.Id);
        }TakePage
        data = data.Skip((page - 1) * pagesize).Take(pagesize);
        //Console
        foreach (var item in data)
        {
            Console.WriteLine("{0},{1},{2}", item.Id, item.Name, item.Created);
        }
    }

    Friday, December 25, 2015 8:58 AM