从列表中一次处理N个字符串
问题描述:
我有一个List<string>
包含N
字符串。从列表中一次处理N个字符串<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<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;
}
就像上面那样?
+1
这个工作,但它重复了300次'MessageBox'。 – Enumy 2014-10-03 23:48:55
你是什么意思'在xList' 100S数,你能否详细说明? – Ofiris 2014-10-03 23:40:09
如果'xList'包含300个字符串,那么'count'将是3 – Enumy 2014-10-03 23:42:23
如果xList包含300,则count为3.如果xList包含350,那么count是多少? 3或4? – 2014-10-03 23:47:19