2014-10-03 33 views
0

我有一個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

回答

2

簡化版用更少的變量和循環

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

1
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

相關問題