Can we control LINQ expression order with Skip(), Take() and OrderBy()

后端 未结 6 773
日久生厌
日久生厌 2021-02-08 14:16

I\'m using LINQ to Entities to display paged results. But I\'m having issues with the combination of Skip(), Take() and OrderBy() calls.

6条回答
  •  太阳男子
    2021-02-08 15:01

    Assuming from you comment the persisting the values in a List is not acceptable:

    There's no way to completely minimize the iterations, as you intended (and as I would have tried too, living in hope). Cutting the iterations down by one would be nice. Is it possible to just get the Count once and cache/session it? Then you could:

    int total = ctx.EntitySet.Count;  // Hopefully you can not repeat doing this.
    var result = ctx.EntitySet.Where(/* filter */).OrderBy(/* expression */).Skip(n).Take(x).ToList();
    

    Hopefully you can cache the Count somehow, or avoid needing it every time. Even if you can't, this is the best you can do.

提交回复
热议问题