Windows Phone 7 IEnumerable.Select和SelectMany的區別

來源:互聯網
上載者:User

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; }

}
}
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.