Linq SelectMany和Select的區別
阿新 • • 發佈:2019-01-25
//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); }