Entity Framework/Linq to SQL: Skip & Take

后端 未结 6 2476
一个人的身影
一个人的身影 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:25

    I created simple extension:

    public static IEnumerable SelectPage(this IEnumerable list, Func sortFunc, bool isDescending, int index, int length)
    {
        List result = null;
        if (isDescending)
            result = list.OrderByDescending(sortFunc).Skip(index).Take(length).ToList();
        else
            result = list.OrderBy(sortFunc).Skip(index).Take(length).ToList();
        return result;
    }
    

    Simple use:

    using (var context = new TransportContext())
    {
        var drivers = (from x in context.Drivers where x.TransportId == trasnportId select x).SelectPage(x => x.Id, false, index, length).ToList();
    }
    

提交回复
热议问题