回傳序列元素 (Select Many)
使用兩個From , 達到 SelectMany的效果。
//寫法2:SelectMany
WriteLine("=寫法2=");
IEnumerable<string> words2 = sentence.SelectMany(segment => segment.Split(' '));
foreach (string w in words2)
{
WriteLine(w);
}
//寫法3:用查詢表達式,達到寫法2-SelectMany的效果,我比較喜歡寫法3
WriteLine("=寫法3=");
IEnumerable<string> words3 =
from segment in sentence
from w in segment.Split(' ')
select w;
foreach (string w in words3)
{
WriteLine(w);
}
留言
張貼留言