2016-09-29 34 views

回答

2

你可以使用LINQ查詢來獲取在兩個列表中值...

List<int> a = new List<int> {1,2,3}; 
List<int> b = new List<int> {2,4,6,3}; 

var matches = a.Intersect(b); 

// Create comma-separated string of matching values... 
string output = string.Join(",", matches); 
+0

所以它會像 'code' VAR匹配= a.Intersect(B)的ToString; String output = string.Join(「,」,matches); label3.Text = Convert.ToString(matches); 'code' ? – DKFrederik

+0

沒有。 ToString不會幫你在這裏。您可以遍歷匹配列表並創建您自己的逗號分隔字符串,但我的示例中最後一行代碼完全是這樣。瀏覽調試器中的代碼並查看匹配和輸出的內容,您將看到發生了什麼。 – dazedandconfused

+0

耶穌.....坦克很多! 我的問題是,我在比較之前清除了A和B ...太多看相同的代碼太久了:-) 非常感謝您的幫助! – DKFrederik