
- C# Basic Tutorial
- C# - Home
- C# - Overview
- C# - Environment
- C# - Program Structure
- C# - Basic Syntax
- C# - Data Types
- C# - Type Conversion
- C# - Variables
- C# - Constants
- C# - Operators
- C# - Decision Making
- C# - Loops
- C# - Encapsulation
- C# - Methods
- C# - Nullables
- C# - Arrays
- C# - Strings
- C# - Structure
- C# - Enums
- C# - Classes
- C# - Inheritance
- C# - Polymorphism
- C# - Operator Overloading
- C# - Interfaces
- C# - Namespaces
- C# - Preprocessor Directives
- C# - Regular Expressions
- C# - Exception Handling
- C# - File I/O
- C# Advanced Tutorial
- C# - Attributes
- C# - Reflection
- C# - Properties
- C# - Indexers
- C# - Delegates
- C# - Events
- C# - Collections
- C# - Generics
- C# - Anonymous Methods
- C# - Unsafe Codes
- C# - Multithreading
- C# Useful Resources
- C# - Questions and Answers
- C# - Quick Guide
- C# - Useful Resources
- C# - Discussion
How to flatten a list using LINQ C#?
Flattening a list means converting a List<List<T>> to List<T>. For example, let us consider a List<List<int>> which needs to be converted to List<int>.
The SelectMany in LINQ is used to project each element of a sequence to an IEnumerable<T> and then flatten the resulting sequences into one sequence. That means the SelectMany operator combines the records from a sequence of results and then converts it into one result.
Using SelectMany
Example
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 } }; var numList = listOfNumLists.SelectMany(i => i); Console.WriteLine("Numbers in the list:"); foreach(var num in numList){ Console.WriteLine(num); } Console.ReadLine(); } } }
Output
Numbers in the list: 1 2 3 4
Using Query
Example
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 } }; var numList = from listOfNumList in listOfNumLists from value in listOfNumList select value; Console.WriteLine("Numbers in the list:"); foreach(var num in numList){ Console.WriteLine(num); } Console.ReadLine(); } } }
Output
Numbers in the list: 1 2 3 4
- Related Articles
- Python Program to Flatten a Nested List using Recursion
- Python Program to Flatten a List without using Recursion
- How to flatten a shallow list in Python?
- Python - Ways to flatten a 2D list
- How to use LINQ to sort a list in C#?
- Python program to Flatten Nested List to Tuple List
- How to Flatten a Matrix using numpy in Python?
- Flatten a multilevel linked list in C++
- Flatten Tuples List to String in Python
- Flatten Binary Tree to Linked List in C++
- Flatten tuple of List to tuple in Python
- How to flatten a Docker image?
- Flatten Nested List Iterator in Python
- Flatten given list of dictionaries in Python
- How to perform a left outer join using linq extension methods in C#?

Advertisements