From the course: Advanced C#: Functional Programming Patterns

Unlock this course with a free trial

Join today to access over 24,500 courses taught by industry experts.

Join with SelectMany

Join with SelectMany

Here's another use for SelectMany. It's useful for creating Cartesian products from the two sequences. Now I'm using a simpler example here. I've got an immutable list of integers called odds and an immutable list of integers called evens. Before I show you the Cartesian product, let's look at just a basic concat method. There is a concat available in LINQ, and all that does is work with two flat sequences and it joins them end-to-end. I'll end up with a list here that contains six items: 3, 5, 7, and then 12, 14, and 16. If I want to perform an operation, remember, that's why we use select. We can perform an operation and we can transform it. I could transform the two lists into something else. In this case, it's a string. It's going to have the first number, 3. I'll say A, 3, and then B, 12, and then A, 3; B, 14, and so on. That's the Cartesian product. The way I work with that is shown here. So I call SelectMany. Now I've got the instructions or I should say the comments of what…

Contents