我想实现简单的分页。偏移foreach循环
我目前有一个Dictionary
,并通过循环遍历foreach
循环在页面上显示其内容。
我找不到方法来抵消foreach
循环。
比方说,我有100个项目。每页5个项目,共20页。我将从以下几点入手:
int counter = 0;
int itemsPerPage = 5;
int totalPages = (items.Count - 1)/itemsPerPage + 1;
int currentPage = (int)Page.Request.QueryString("page"); //assume int parsing here
Dictionary<string, string> currentPageItems = new Dictionary<string, string>;
foreach (KeyValuePair<string, string> item in items) //items = All 100 items
{
//---Offset needed here----
currentPageItems.Add(item.Key, item.Value);
if (counter >= itemsPerPage)
break;
counter++;
}
这将输出第一页正确的 - 现在我怎么显示后续页面?
我喜欢你的解释,你的'.ToDictionary'扩展。另外你需要的声誉比其他人在这里:-) –
同情投票!哇噢! :) – mjwills
@moontear - 不错的一个,我喜欢你接受最低信誉评分的人的答案。好想法。 – ColinE