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
What is the difference between Select and SelectMany in Linq C#?
The Select and SelectMany operators in LINQ C# serve different purposes for data projection. Select produces one result value for every source element, while SelectMany belongs to Projection Operators category and is used to project each element of a sequence to an IEnumerable and flattens the resulting sequences into one sequence.
Syntax
Following is the syntax for Select operator −
IEnumerable<TResult> Select<TSource, TResult>( this IEnumerable<TSource> source, Func<TSource, TResult> selector )
Following is the syntax for SelectMany operator −
IEnumerable<TResult> SelectMany<TSource, TResult>( this IEnumerable<TSource> source, Func<TSource, IEnumerable<TResult>> selector )
Key Differences
| Select | SelectMany |
|---|---|
| Projects each element to a new form (1-to-1 mapping) | Projects and flattens nested collections (1-to-many mapping) |
| Returns IEnumerable<T> where T can be any type | Returns IEnumerable<T> where T is the flattened element type |
| Maintains the original structure | Flattens nested structures into a single sequence |
Using Select Operator
Select transforms each element but maintains the structure. If the source contains collections, Select returns nested collections −
Example
using System;
using System.Collections.Generic;
using System.Linq;
class Demo {
public string Name { get; set; }
public List<string> Contents { get; set; }
public static List<Demo> GetAllContents() {
List<Demo> listContents = new List<Demo> {
new Demo {
Name = "Cap",
Contents = new List<string> { "Nike", "Adidas" }
},
new Demo {
Name = "Shoes",
Contents = new List<string> { "Nike", "Puma", "Adidas" }
}
};
return listContents;
}
}
class Program {
static void Main() {
IEnumerable<List<string>> result = Demo.GetAllContents().Select(s => s.Contents);
Console.WriteLine("Using Select (nested structure):");
foreach (List<string> stringList in result) {
foreach (string str in stringList) {
Console.WriteLine(str);
}
}
}
}
The output of the above code is −
Using Select (nested structure): Nike Adidas Nike Puma Adidas
Using SelectMany Operator
SelectMany flattens nested collections into a single sequence, eliminating the need for nested loops −
Example
using System;
using System.Collections.Generic;
using System.Linq;
class Demo {
public string Name { get; set; }
public List<string> Contents { get; set; }
public static List<Demo> GetAllContents() {
List<Demo> listContents = new List<Demo> {
new Demo {
Name = "Cap",
Contents = new List<string> { "Nike", "Adidas" }
},
new Demo {
Name = "Shoes",
Contents = new List<string> { "Nike", "Puma", "Adidas" }
}
};
return listContents;
}
}
class Program {
static void Main() {
IEnumerable<string> resultSelectMany = Demo.GetAllContents().SelectMany(s => s.Contents);
Console.WriteLine("Using SelectMany (flattened):");
foreach (string str in resultSelectMany) {
Console.WriteLine(str);
}
}
}
The output of the above code is −
Using SelectMany (flattened): Nike Adidas Nike Puma Adidas
Practical Comparison
Example
using System;
using System.Collections.Generic;
using System.Linq;
class Program {
static void Main() {
var data = new List<List<int>> {
new List<int> { 1, 2, 3 },
new List<int> { 4, 5 },
new List<int> { 6, 7, 8, 9 }
};
var selectResult = data.Select(x => x);
var selectManyResult = data.SelectMany(x => x);
Console.WriteLine("Select result type: " + selectResult.GetType().Name);
Console.WriteLine("SelectMany result type: " + selectManyResult.GetType().Name);
Console.WriteLine("\nSelect count: " + selectResult.Count());
Console.WriteLine("SelectMany count: " + selectManyResult.Count());
Console.WriteLine("\nSelectMany flattened values:");
foreach (int value in selectManyResult) {
Console.Write(value + " ");
}
}
}
The output of the above code is −
Select result type: SelectIterator`2 SelectMany result type: SelectManyIterator`2 Select count: 3 SelectMany count: 9 SelectMany flattened values: 1 2 3 4 5 6 7 8 9
Conclusion
Select performs 1-to-1 transformations preserving the original structure, while SelectMany flattens nested collections into a single sequence. Use Select for simple projections and SelectMany when you need to work with flattened data from nested collections.
