且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Windows Phone 7 IEnumerable.Select和SelectMany的区别

更新时间:2022-09-26 11:20:01

IEnumerable<T>在Windows Phone 7的程序上很常用,它允许开发人员定义foreach语句功能的实现并支持非泛型方法的简单迭代,下面主要分析一下 IEnumerable<T>.Select和IEnumerable<T>.SelectMany这两个方法的区别。

IEnumerable<T>.Select 将序列中的每个元素投影到新表中。

IEnumerable<T>.SelectMany 将序列的每个元素投影到 IEnumerable<T> 并将结果序列合并为一个序列。

SelectMany 方法枚举输入序列,使用转换函数将每个元素映射到 IEnumerable<T>,然后枚举并生成每个这种 IEnumerable<T> 对象的元素。 也就是说,对于 source 的每个元素,selector 被调用,返回一个值的序列。 然后 SelectMany将集合的此二维集合合并为一维 IEnumerable<T> 并将其返回。

下面一个小例子用IEnumerable<T>.Select和IEnumerable<T>.SelectMany实现同样的功能,看看两者的区别。

 

Windows Phone 7 IEnumerable<T>.Select和SelectMany的区别 

 


  1. <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0"> 
  2.             <ListBox  Name="Output"      
  3.                       HorizontalAlignment="Stretch"        
  4.                       VerticalAlignment="Stretch"       
  5.                       Margin="10" /> 
  6.             <ListBox  Name="Output2"      
  7.                       HorizontalAlignment="Stretch"        
  8.                       VerticalAlignment="Stretch"       
  9.                       Margin="200,10,10,10" /> 
  10. </Grid> 

cs页面

 


  1. namespace LinqApplication  
  2. {  
  3.     public partial class MainPage : PhoneApplicationPage  
  4.     {  
  5.         List<Book> Books;//List<T>继承了IEnumerable<T> 接口  
  6.         public MainPage()  
  7.         {  
  8.             InitializeComponent();  
  9.             CreateBooks();  
  10.             //IEnumerable<Book>.Select 将序列中的Authors元素投影到新表中。  
  11.             IEnumerable<List<Author>> EnumerableOfListOfAuthors = Books.Select(book => book.Authors);  
  12.  
  13.             foreach (var listOfAuthors in EnumerableOfListOfAuthors)  
  14.             {   
  15.                 foreach (Author auth in listOfAuthors)  
  16.                 {  
  17.                     Output.Items.Add(auth.Name); //添加到ListBox里面  
  18.                 }  
  19.             }  
  20.  
  21.             //IEnumerable<Book>.SelectMany  将序列的每个元素投影到 IEnumerable<T> 并将结果序列合并为一个序列。  
  22.             IEnumerable<Author> authors = Books.SelectMany(book => book.Authors);  
  23.             foreach (Author auth in authors)   
  24.             {   
  25.                 Output2.Items.Add(auth.Name);  
  26.             }   
  27.  
  28.  
  29.         }  
  30.  
  31.         private void CreateBooks()   
  32.         {   
  33.             Books = new List<Book>();   
  34.             Author auth1 = new Author() { Name = "张三"Age = 32 };  
  35.             Author auth2 = new Author() { Name = "李四"Age = 30 };  
  36.             Author auth3 = new Author() { Name = "加菲猫"Age = 31 };  
  37.             List<Author> authors = new List<Author>() { auth1, auth2, auth3 };  
  38.  
  39.             Book newnewBook = new Book() { Authors = authorsNumPages = 500Title = "Programming C#" };  
  40.             Books.Add(newBook); auth1 = new Author() { Name = "刘德华"Age = 42 };  
  41.  
  42.             authors = new List<Author>() { auth1 };  
  43.             newnewBook = new Book() { Authors = authorsNumPages = 350Title = "Book 2" };  
  44.             Books.Add(newBook); auth1 = new Author() { Name = "周杰伦"Age = 32 };  
  45.  
  46.             auth2 = new Author() { Name = "林志玲"Age = 32 };   
  47.             authors = new List<Author>() { auth1, auth2 };  
  48.             newnewBook = new Book() { Authors = authorsNumPages = 375Title = "Programming with WP7" };   
  49.             Books.Add(newBook);  
  50.         }   
  51.     }  

Author.cs类

 


  1. namespace LinqApplication  
  2. {  
  3.     public class Author  
  4.     {  
  5.         public string Name;   
  6.         public int Age;   
  7.     }  

Book.cs类

 


  1. namespace LinqApplication  
  2. {  
  3.     public class Book  
  4.     {  
  5.         public String Title { get; set; }   
  6.         public List<Author> Authors { get; set; }   
  7.         public int NumPages { get; set; }   
  8.  
  9.     }  

 



本文转自linzheng 51CTO博客,原文链接:http://blog.51cto.com/linzheng/1078631