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

时间:2021-04-18 08:53:01

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 items table by description. I can easily do:

假设我想按描述搜索项目表。我可以轻松地做到:

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 count query 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. 我应该在执行此操作之前执行计数查询以查找结果集大小,然后根据我的需要限制此查询吗?我觉得这是要走的路。
  3. 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 SQL doing some magic here?
  4. 我应该执行完整查询,从数组大小中获取计数并仅返回此数组中的分页子集吗?如果结果集足够大,我觉得这会浪费很多时间......或者LINQ to SQL在这里做了些什么?

Is there a LINQ to SQL common pattern for performing this operation?

是否有LINQ to SQL常用模式来执行此操作?

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

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

2 个解决方案

#1


30  

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显示带有“Page X of Y”的导航栏...第1页,共10页等。

#2


2  

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":

您可以更进一步,并使用Skip进行后续“页面”:

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);
}

#1


30  

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显示带有“Page X of Y”的导航栏...第1页,共10页等。

#2


2  

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":

您可以更进一步,并使用Skip进行后续“页面”:

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);
}

相关文章