从列表中一次处理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

有没有更简单的方法来做到这一点?

+0

你是什么意思'在xList' 100S数,你能否详细说明? – Ofiris 2014-10-03 23:40:09

+0

如果'xList'包含300个字符串,那么'count'将是3 – Enumy 2014-10-03 23:42:23

+0

如果xList包含300,则count为3.如果xList包含350,那么count是多少? 3或4? – 2014-10-03 23:47:19

简化版用更少的变量和循环

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; 
} 
+1

完美地工作,谢谢。 – Enumy 2014-10-03 23:50:26

+1

为了完整性,将for循环中的'100'幻数改为使用'N'变量。 – DavidG 2014-10-04 00:27:30

+1

@DavidG,感谢您的评论,我确实改变了它。 – Ofiris 2014-10-04 08:07:05

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