IEnumerable.Select和SelectMany的区别
来源:互联网 发布:手机屏幕坐标软件 编辑:程序博客网 时间:2024/05/23 12:42
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实现同样的功能,看看两者的区别。
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0"> <ListBox Name="Output" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Margin="10" /> <ListBox Name="Output2" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Margin="200,10,10,10" /></Grid>
cs页面
namespace LinqApplication{ public partial class MainPage : PhoneApplicationPage { List<Book> Books;//List<T>继承了IEnumerable<T> 接口 public MainPage() { InitializeComponent(); CreateBooks(); //IEnumerable<Book>.Select 将序列中的Authors元素投影到新表中。 IEnumerable<List<Author>> EnumerableOfListOfAuthors = Books.Select(book => book.Authors); foreach (var listOfAuthors in EnumerableOfListOfAuthors) { foreach (Author auth in listOfAuthors) { Output.Items.Add(auth.Name); //添加到ListBox里面 } } //IEnumerable<Book>.SelectMany 将序列的每个元素投影到 IEnumerable<T> 并将结果序列合并为一个序列。 IEnumerable<Author> authors = Books.SelectMany(book => book.Authors); foreach (Author auth in authors) { Output2.Items.Add(auth.Name); } } private void CreateBooks() { Books = new List<Book>(); Author auth1 = new Author() { Name = "张三", Age = 32 }; Author auth2 = new Author() { Name = "李四", Age = 30 }; Author auth3 = new Author() { Name = "加菲猫", Age = 31 }; List<Author> authors = new List<Author>() { auth1, auth2, auth3 }; Book newBook = new Book() { Authors = authors, NumPages = 500, Title = "Programming C#" }; Books.Add(newBook); auth1 = new Author() { Name = "刘德华", Age = 42 }; authors = new List<Author>() { auth1 }; newBook = new Book() { Authors = authors, NumPages = 350, Title = "Book 2" }; Books.Add(newBook); auth1 = new Author() { Name = "周杰伦", Age = 32 }; auth2 = new Author() { Name = "林志玲", Age = 32 }; authors = new List<Author>() { auth1, auth2 }; newBook = new Book() { Authors = authors, NumPages = 375, Title = "Programming with WP7" }; Books.Add(newBook); } }}
Author.cs类
namespace LinqApplication{ public class Author { public string Name; public int Age; }}
Book.cs类
namespace LinqApplication{ public class Book { public String Title { get; set; } public List<Author> Authors { get; set; } public int NumPages { get; set; } }}
- IEnumerable.Select和SelectMany的区别
- IEnumerable.Select和SelectMany的区别
- Linq SelectMany和Select的区别
- linq里的select和selectmany操作
- Linq SelectMany和Select的用法
- Select和selectMany
- IEnumerable和IQueryable的区别
- IQueryable和IEnumerable的区别
- IEnumerable和 IEnumerator的区别
- IQueryable和IEnumerable的区别
- IEnumerator和IEnumerable的区别
- Select & SelectMany
- IEnumerable.Select和where
- linq里的IEnumerable和IQueryable区别
- IQueryable<T>和IEnumerable<T>的区别
- C# IQueryable和IEnumerable的区别
- IEnumerable和IEnumerator区别
- IEnumerable和IEnumerator区别
- 易语言写程序很恼人……
- 日志模块(一头文件就实现了日志记录)
- 非对称加密算法-ElGamal算法
- 与linux有关的机构名称及标准
- 标准C++的类型转换符:static cast、dynamic cast、reinterpret cast和const cast
- IEnumerable.Select和SelectMany的区别
- vb数据库编程(五)--DAO对象
- AccsessHelper类编写成功
- Android开发指导文档(译)--content provider(一)
- opensuse 11.4安装 ibus
- Zencart网站迁移后,需要改动的配置文件
- 关于多继承层次的set魔术方法解析
- mourning the loss of David Brownell
- Chain Of Responsibility模式