linq Select与SelectMany的区别

elect() 为每个源值生成一个结果值。因此,总体结果是一个与源集合具有相同元素数目的集合。与之相反,SelectMany() 将生成单一总体结果,其中包含来自每个源值的串联子集合。作为参数传递到 SelectMany() 的转换函数必须为每个源值返回一个可枚举值序列。然后,SelectMany() 将串联这些可枚举序列以创建一个大的序列。

string[] text ={ "Albert was here", "Burke slept late", "Connor is happy" };  

var tokens = text.Select(s => s.Split(''));

 foreach (string[] line in tokens)

      foreach (string token in line)        

      Console.Write("{0}.", token);


 
string[] text ={ "Albert was here", "Burke slept late", "Connor is happy" };  
var tokens = text.SelectMany(s => s.Split(''));  

foreach (string token in tokens)    

  Console.Write("{0}.", token);

用select的时候断点如图:

 

 

用SelectMany的的调试结果:

 

从断点调试的结果我们可以看出:

 

 Select() 为每个源值生成一个结果值。 因此,总体结果是一个与源集合具有相同元素数目的集合。 与之相反,SelectMany() 将生成单一总体结果,其中包含来自每个源值的串联子集合。 

 

posted @ 2020-11-05 15:46  Tim1027  阅读(573)  评论(0编辑  收藏  举报