Entity Framework/Linq to SQL: Skip & Take

后端 未结 6 2486
一个人的身影
一个人的身影 2020-11-27 15:19

Just curious as to how Skip & Take are supposed to work. I\'m getting the results I want to see on the client side, but when I hook up the AnjLab SQL Profiler and look a

6条回答
  •  我在风中等你
    2020-11-27 15:36

    Try this:

    public IEnumerable ListStores(Func sort, bool desc, int page, int pageSize, out int totalRecords)
    {
        var context = new TectonicEntities();
        var results = context.Stores;
    
        totalRecords = results.Count();
        int skipRows = (page - 1) * pageSize;
    
        if (desc)
            results = results.OrderByDescending(sort);
    
        return results.Skip(skipRows).Take(pageSize).ToList();
    }
    

    in truth, that last .ToList() isn't really necessary as you are returning IEnumerable...

    There will be 2 database calls, one for the count and one when the ToList() is executed.

提交回复
热议问题