IEnumerable.Select和SelectMany的區別


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

    }
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM