本文介绍了使用LINQ分页对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你会如何在LINQ查询实现分页?其实暂时,我会满意,如果SQL TOP功能可以模仿。但是,我相信,有必要充分分页支持上来以后迟早反正。

  VAR QueryResult中=邻的对象
                  哪里 ...
                  选择新
                      {
                         A = o.a,
                         B = o.b
                      }
                   ????????? TOP 10 ????????
 

解决方案

您正在寻找的跳过扩展方法。 移动过去在结果中前N个元素,返回其余部分; 返回结果的第N个元素,丢弃任何剩余的元素。

有关如何使用这些方法的更多信息,请参阅MSDN:http://msdn.microsoft.com/en-us/library/bb386988.aspx

例如:

  INT numberOfObjectsPerPage = 10;
VAR queryResultPage = QueryResult中
  .Skip(numberOfObjectsPerPage *页面编号)
  。取(numberOfObjectsPerPage);
 

How would you implement paging in a LINQ query? Actually for the time being, I would be satisfied if the sql TOP function could be imitated. However, I am sure that the need for full paging support comes up sooner later anyway.

var queryResult = from o in objects
                  where ...
                  select new
                      {
                         A = o.a,
                         B = o.b
                      }
                   ????????? TOP 10????????
解决方案

You're looking for the Skip and Take extension methods. Skip moves past the first N elements in the result, returning the remainder; Take returns the first N elements in the result, dropping any remaining elements.

See MSDN for more information on how to use these methods: http://msdn.microsoft.com/en-us/library/bb386988.aspx

For example:

int numberOfObjectsPerPage = 10;
var queryResultPage = queryResult
  .Skip(numberOfObjectsPerPage * pageNumber)
  .Take(numberOfObjectsPerPage);

这篇关于使用LINQ分页对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-27 11:50