2013-06-23 49 views
1

智能的方式說我有兩套到Concat的兩套

var Set1 = new[] { "A", "B", "C" }; 
var Set2 = new[] { "1", "2", "3" }; 

要Concat的這兩套通常我

foreach (string itm1 in Set1) 
{ 
    foreach (string itm2 in Set2) 
    { 
     Console.WriteLine(itm1 + itm2); 
    } 
} 

有沒有得到同樣結果的任何LINQ風格?

+0

的LINQ的ZIP可以幫助這裏 –

回答

0

嘗試

var concatSet =Set1 
       .SelectMany(
          item => Set2, 
          (i1, i2) => (i1 + i2) 
          ); 
2
var strs = Set1.SelectMany(s1 => Set2.Select(s2 => s1 + s2)) 
foreach(string s in strs) 
{ 
    Console.WriteLine(s); 
} 

或者使用查詢語法:

var strs = from s1 in Set1 
      from s2 in Set2 
      select s1 + s2;