C# 使用 LINQ to SQL 对搜索结果进行分页

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/793718/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 01:24:33  来源:igfitidea点击:

Paginated search results with LINQ to SQL

c#linqlinq-to-sqlpagination

提问by Pablo Santa Cruz

What's the best pattern to get paginated results with LINQ to SQL?

使用LINQ to SQL获得分页结果的最佳模式是什么?

I have the following scenario:

我有以下场景:

Suppose I want to search itemstable by description. I can easily do:

假设我想通过description搜索items表。我可以轻松做到:

public IQueryable<Item> FindItemsByDescription(string description)
{
   return from item in _dc.Items
          where item.Description.Contains(description);
}

Now, what would be the best way to paginate this result set?

现在,对这个结果集进行分页的最佳方是什么?

  1. Should I perform a countquery before doing this to find out the result set size and then limit this query according to what I want? I feel like this is the way to go.
  2. Should I perform the full query, take the count from the array size and return only a paginated subset from this array? I feel like this will be a huge waste of time if the resultset is big enough... Or is LINQ to SQLdoing some magic here?
  1. 我是否应该在执行此操作之前执行计数查询以找出结果集大小,然后根据我的需要限制此查询?我觉得这是要走的路。
  2. 我是否应该执行完整查询,从数组大小中获取计数并仅从该数组中返回一个分页子集?如果结果集足够大,我觉得这将是一个巨大的浪费......或者LINQ to SQL在这里做了一些魔术?

Is there a LINQ to SQLcommon pattern for performing this operation?

是否有用于执行此操作的LINQ to SQL通用模式?

EDIT: I mustclarify a one little thing. I am aware of Take and Skip methods. But, before using Takeand Skip, how should I get the total countof results that query would retrieve?

编辑:我必须澄清一件小事。我知道 Take 和 Skip 方。但是,在使用TakeSkip之前,我应该如何获得查询将检索到的结果总数

采纳答案by Jose Basilio

The pattern for paging is very simple. It involves the use of the Skip() and Take() extension methods as follows:

分页模式非常简单。它涉及到 Skip() 和 Take() 扩展方的使用,如下所示:

public IQueryable<Item> FindItemsByDescription(string description, int pageIndex, int pageSize)
{
   return from item in _dc.Items
          where item.Description.
          Contains(description).
          Skip((pageIndex - 1) * pageSize).
          Take(pageSize);
}

UPDATE:To get the total count simply use the Count() method:

更新:要获得总计数,只需使用 Count() 方:

int totalCount = from item in _dc.Items
                 where item.Description.
                 Contains(description).Count();

int numberOfPages = (int)(totalCount/pageSize);

Depending on how you are going to the display the records, you can use the numberOfPages to display a navigation bar with "Page X of Y" ... Page 1 of 10, etc..

根据您要显示记录的方式,您可以使用 numberOfPages 来显示带有“第 X 页,共 Y 页”的导航栏......第 1 页,共 10 页,等等。

回答by BFree

You can use the Take extension method:

您可以使用 Take 扩展方:

public IQueryable<Item> FindItemsByDescription(string description, int resultAmount)
{
   return from item in _dc.Items
          where item.Description.Contains(description).Take(resultAmount);
}

You can take this one step further and use Skip for subsequent "pages":

您可以更进一步,对后续的“页面”使用跳过:

public IQueryable<Item> FindItemsByDescription(string description, int resultAmount, int page)
{
   return from item in _dc.Items
          where item.Description.Contains(description).Skip(resultAmount * page).Take(resultAmount);
}