一次处理来自List<;字符串>;

本文关键字:List gt lt 字符串 一次 处理 | 更新日期: 2023-09-27 18:25:27

我有一个包含N字符串的List<string>

我需要一次处理NN字符串,然后处理最后几个不等于NN的字符串。

示例:

List<string> xList; //Contains 300 string
int N = 100;
int count = //Number of 100s in xList > Couldn't figure it out yet
int counter = 0;
for (int i = 1; i < = count; i++)
{
    var vList = xList.Skip(counter).Take(N);
    MessageBox.Show(string.Join(Environment.NewLine, vList.ToArray()));
    counter += N;
}

如果xList包含350呢?

有什么更简单的方法可以做到这一点吗?

一次处理来自List<;字符串>;

具有较少变量和循环的简化版本:

List<string> xList; //Contains 350 string
int N = 100;
for (int i = 0; i <= xList.Count; i += N)
{
    var vList = xList.Skip(i).Take(N);
    MessageBox.Show(string.Join(Environment.NewLine, vList.ToArray()));
    counter += N;
}
List<string> xList; //Contains 300 string
int N = 100;
int counter = 0;
for (int i = 1; i < = xList.Count; i++)
{
    var vList = xList.Skip(counter).Take(N);
    MessageBox.Show(string.Join(Environment.NewLine, vList.ToArray()));
    counter += N;
}

像上面这样的东西?