Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to flatten a list using LINQ C#?
Flattening a list means converting a List<List<T>> to List<T>. For example, converting a List<List<int>> containing multiple integer lists into a single List<int> with all elements combined.
The SelectMany operator in LINQ is used to project each element of a sequence to an IEnumerable<T> and then flatten the resulting sequences into one sequence. It combines records from a sequence of results and converts them into a single flattened result.
Syntax
The basic syntax for flattening using SelectMany −
var flattened = nestedList.SelectMany(innerList => innerList);
Using LINQ query syntax −
var flattened = from outerList in nestedLists
from item in outerList
select item;
Using SelectMany Method
The SelectMany method provides a direct way to flatten nested collections −
using System;
using System.Collections.Generic;
using System.Linq;
namespace DemoApplication {
public class Program {
static void Main(string[] args) {
List<List<int>> listOfNumLists = new List<List<int>> {
new List<int> { 1, 2 },
new List<int> { 3, 4 },
new List<int> { 5, 6 }
};
var numList = listOfNumLists.SelectMany(i => i);
Console.WriteLine("Numbers in the flattened list:");
foreach(var num in numList) {
Console.WriteLine(num);
}
}
}
}
The output of the above code is −
Numbers in the flattened list: 1 2 3 4 5 6
Using LINQ Query Syntax
You can also use LINQ query syntax with multiple from clauses to achieve the same result −
using System;
using System.Collections.Generic;
using System.Linq;
namespace DemoApplication {
public class Program {
static void Main(string[] args) {
List<List<int>> listOfNumLists = new List<List<int>> {
new List<int> { 1, 2 },
new List<int> { 3, 4 },
new List<int> { 5, 6 }
};
var numList = from listOfNumList in listOfNumLists
from value in listOfNumList
select value;
Console.WriteLine("Numbers in the flattened list:");
foreach(var num in numList) {
Console.WriteLine(num);
}
}
}
}
The output of the above code is −
Numbers in the flattened list: 1 2 3 4 5 6
Flattening with Transformation
You can combine flattening with data transformation by providing a selector function −
using System;
using System.Collections.Generic;
using System.Linq;
namespace DemoApplication {
public class Program {
static void Main(string[] args) {
List<List<string>> wordLists = new List<List<string>> {
new List<string> { "hello", "world" },
new List<string> { "linq", "rocks" }
};
var upperCaseWords = wordLists.SelectMany(list => list.Select(word => word.ToUpper()));
Console.WriteLine("Flattened and uppercase words:");
foreach(var word in upperCaseWords) {
Console.WriteLine(word);
}
}
}
}
The output of the above code is −
Flattened and uppercase words: HELLO WORLD LINQ ROCKS
Comparison of Approaches
| Method | Syntax | Use Case |
|---|---|---|
| SelectMany | list.SelectMany(x => x) | Simple flattening, method chaining |
| Query Syntax | from x in list from y in x select y | Complex queries, SQL-like syntax |
| SelectMany with Transform | list.SelectMany(x => x.Select(...)) | Flattening with data transformation |
Conclusion
LINQ's SelectMany operator is the primary method for flattening nested collections in C#. It can be used with both method syntax and query syntax, and supports transformation during the flattening process. Choose the approach that best fits your coding style and requirements.
