From the course: Complete Guide to .NET LINQ: Querying Collections, Databases, and Markup

Unlock this course with a free trial

Join today to access over 25,300 courses taught by industry experts.

Flattening sequences with SelectMany

Flattening sequences with SelectMany

- [Instructor] We've used the select clause or the select extension method a lot in this course. In this video, I want to introduce a related method called SelectMany. I've included it in the grouping chapter because in a way, it does the opposite of grouping. While grouping organizes items into collections, SelectMany flattens them into a single sequence. Let's think about groups for a moment. When you create a group, you get a one to many relationship. A group containing multiple values. SelectMany also works with a one to many relationship. But instead of keeping the group separate, it flattens all the values into a single sequence. Now, in this example, I'm not using a group, but I am using a type that has a nested list. So let's take a look at what I have here. On line 28, I have the HistoryCalendar class. It has a string property that represents the name of the decade, and then it has a list of ints that represents the years in that decade. Then up here in my code, I'm…

Contents