In this tutorial , i’ll show you Select and SelectMany Operator with C# Examples.
Select and SelectMany, both are projection operator, that means, it selects value from the list, collection or other source.
Select operator selects values from a collection whereas SelectMany Operator selects values from multiple collection or nested collection. SelectMany Operator selects values from multiple or nested collection and flatten the result.
You can understand Select and SelectMany Operator in LINQ more clearly when you will see the programming example.
C# LINQ Example 1:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
using System; using System.Collections.Generic; using System.Linq; namespace LinqTutorial { class ProductStore { public string productName { get; set; } public int productPrice { get; set; } public List<string> Size { get; set; } } class Program { static void Main(string[] args) { var result = from p in GetProductDetails() select new { p.productName, p.productPrice, p.Size }; foreach (var r in result) { Console.WriteLine(r); } Console.ReadKey(); } //Creating List of Product static List<ProductStore> GetProductDetails() { List<ProductStore> product = new List<ProductStore> { new ProductStore { productName = "HardDisk", productPrice = 3400, Size = new List<string>{"240GB","500GB","1TB"} }, new ProductStore { productName = "RAM", productPrice = 7500, Size = new List<string>{"4GB","8GB","16GB"} }, new ProductStore { productName = "Monitor", productPrice = 3400, Size = new List<string>{"14.5 Inch","18 Inch","24 Inch"} } }; return product; } } } |
Output:
1 2 3 4 5 |
{ productName = HardDisk, productPrice = 3400, Size = System.Collections.Generic.List`1[System.String] } { productName = RAM, productPrice = 7500, Size = System.Collections.Generic.List`1[System.String] } { productName = Monitor, productPrice = 3400, Size = System.Collections.Generic.List`1[System.String] } |
C# LINQ Example 2:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
using System; using System.Collections.Generic; using System.Linq; namespace LinqTutorial { class ProductStore { public string productName { get; set; } public int productPrice { get; set; } public List<string> Size { get; set; } } class Program { static void Main(string[] args) { var result = from p in GetProductDetails() select p; foreach (var r in result.SelectMany(ProductStore => ProductStore.Size)) { Console.WriteLine(r); } Console.ReadKey(); } //Creating List of Product static List<ProductStore> GetProductDetails() { List<ProductStore> product = new List<ProductStore> { new ProductStore { productName = "HardDisk", productPrice = 3400, Size = new List<string>{"240GB","500GB","1TB"} }, new ProductStore { productName = "RAM", productPrice = 7500, Size = new List<string>{"4GB","8GB","16GB"} }, new ProductStore { productName = "Monitor", productPrice = 3400, Size = new List<string>{"14.5 Inch","18 Inch","24 Inch"} } }; return product; } } } |
Output:
1 2 3 4 5 6 7 8 9 10 11 |
240GB 500GB 1TB 4GB 8GB 16GB 14.5 Inch 18 Inch 24 Inch |
C# LINQ Example 3:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
using System; using System.Collections.Generic; using System.Linq; namespace LINQDemo { class Program { static void Main(string[] args) { List<string> nameList =new List<string>(){"csharp", "console","examples" }; IEnumerable<char> querySyntax = from str in nameList from ch in str select ch; foreach (char c in querySyntax) { Console.Write(c + " "); } Console.ReadKey(); } } } |
Output:
c s h a r p c o n s o l e e x a m p l e s
C# LINQ Example 4:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
using System; using System.Collections.Generic; using System.Linq; namespace LINQDemo { public class Student { public int ID { get; set; } public string Name { get; set; } public string Email { get; set; } public List<string> Programming { get; set; } public static List<Student> GetStudents() { return new List<Student>() { new Student(){ID = 1, Name = "James", Email = "James@j.com", Programming = new List<string>() { "C#", "Jave", "C++"} }, new Student(){ID = 2, Name = "Sam", Email = "Sara@j.com", Programming = new List<string>() { "WCF", "SQL Server", "C#" }}, new Student(){ID = 3, Name = "Patrik", Email = "Patrik@j.com", Programming = new List<string>() { "MVC", "Jave", "LINQ"} }, new Student(){ID = 4, Name = "Sara", Email = "Sara@j.com", Programming = new List<string>() { "ADO.NET", "C#", "LINQ" } } }; } } class Program { static void Main(string[] args) { //Using Method Syntax List<string> MethodSyntax = Student.GetStudents().SelectMany(std => std.Programming).ToList(); //Using Query Syntax IEnumerable<string> QuerySyntax = from std in Student.GetStudents() from program in std.Programming select program; //Printing the values foreach (string program in MethodSyntax) { Console.WriteLine(program); } Console.ReadKey(); } } } |
Output:
C#
Jave
C++
WCF
SQL Server
C#
MVC
Jave
LINQ
ADO.NET
C#
LINQ