Introduction to LINQ SelectMany
LINQ Selectmany is a Category of Query Operator which comes under Projection Operators. SelectMany operator used to select the elements from collection of collection called Nested Collection. SelectMany returns a single result from a nested collection; the result contains the concatenation elements for each source value. SelectMany operator works with combination of records and returns with single result. Here SelectMany operator combines the records from chain of results and then it converts as a resultant set of single result.
Related Articles
Syntax
Let’s identify with the following syntax for LINQ SelectMany operator,
- public static IEnumerable
SelectMany (this IEnumerable source, Func > selector); - public static IEnumerable
SelectMany (this IEnumerable source, Func > selector); - public static IEnumerable
SelectMany (this IEnumerable source, Func > collectionSelector, Func resultSelector);
In this syntax we are using SelectMany to select from combination of records and returns with single result.
How SelectMany works in LINQ?
In LINQ query operator Select and SelectMany come under the Projection Operator. The Select Operator used to select the values from a collection whereas the SelectMany Operator used to select the values from the nested collection which is called a collection of the collection The SelectMany operator projects the elements from sequence to IEnumerable
Examples
SelectMany operators used to select the elements from a collection of collection called Nested Collection. SelectMany returns a single result from a nested collection; the result contains the concatenation elements for each source value.
Program
In this program, it describes the first syntax as shown above in the syntax heading as follows; let’s see the following program,
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Console_LinQSelectMany
{
//Program_Sample - 1
class Program_LINQ_SelectMany
{
// Getting Employee details in EmployeeClass
public class EmployeeClass
{
public int EmployeeID { get; set; }
public string EmployeeName { get; set; }
public List
}
public class DepartmentClass
{
public string Dept_Name { get; set; }
}
class LinqProgram
{
static public void Main()
{
List
employeesList.Add(new EmployeeClass
{
EmployeeID = 1,
EmployeeName = "Smith",
DepartmentList = new List
{
new DepartmentClass { Dept_Name = "Development" },
new DepartmentClass { Dept_Name = "Human-Resources"}
} });
employeesList.Add(new EmployeeClass
{
EmployeeID = 2,
EmployeeName = "Gorge",
DepartmentList = new List
{
new DepartmentClass { Dept_Name = "Designing" },
new DepartmentClass { Dept_Name = "Business-Analyst"}
}
});
employeesList.Add(new EmployeeClass
{
EmployeeID = 3,
EmployeeName = "Peter",
DepartmentList = new List
{
new DepartmentClass { Dept_Name = "Business-Analyst" },
new DepartmentClass { Dept_Name = "Human-Resources"}
}
});
Console.WriteLine("\nLINQ - SelectMany Method");
Console.WriteLine("-------------------------\n");
Console.WriteLine("Department - Names");
Console.WriteLine("------------------\n");
var getDepartments = employeesList.SelectMany(d => d.DepartmentList);
foreach (var dept in getDepartments)
{
Console.WriteLine(dept.Dept_Name);
}
Console.ReadLine();
}
}
}
}
It is a SelectMany first overloaded method; here it displays the department names linked with each Employee List.
var getDepartments = employeesList.SelectMany(d => d.DepartmentList);
Output:
Program
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Console_LinQSelectMany
{
//Program_Sample - 2
class Program_LINQ_SelectMany
{
// Getting Employee details in EmployeeClass
public class EmployeeClass
{
public int EmployeeID { get; set; }
public string EmployeeName { get; set; }
public List
}
public class DepartmentClass
{
public string Dept_Name { get; set; }
}
class LinqProgram
{
static public void Main()
{
List
employeesList.Add(new EmployeeClass
{
EmployeeID = 1,
EmployeeName = "Smith",
DepartmentList = new List
{
new DepartmentClass { Dept_Name = "Development" },
new DepartmentClass { Dept_Name = "Human-Resources"}
}
});
employeesList.Add(new EmployeeClass
{
EmployeeID = 2,
EmployeeName = "Gorge",
DepartmentList = new List
{
new DepartmentClass { Dept_Name = "Designing" },
new DepartmentClass { Dept_Name = "Business-Analyst"}
}
});
employeesList.Add(new EmployeeClass
{
EmployeeID = 3,
EmployeeName = "Peter",
DepartmentList = new List
{
new DepartmentClass { Dept_Name = "Business-Analyst" },
new DepartmentClass { Dept_Name = "Human-Resources"}
}
});
Console.WriteLine("\nLINQ - SelectMany Method");
Console.WriteLine("-------------------------\n");
Console.WriteLine("Department - Names");
Console.WriteLine("------------------\n");
var getDepartments = employeesList.SelectMany((d, index) => d.DepartmentList.Select(i => index.ToString() + "," + i.Dept_Name));
foreach (var dept in getDepartments)
{
Console.WriteLine("\t"+dept);
}
Console.ReadLine();
}
}
}
}
It is a SelectMany second overloaded method as shown in syntax; here it displays each department name with including the corresponding index value of every object sequence.
Output:
Program
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Console_LinQSelectMany
{
//Program_Sample - 3
class Program_LINQ_SelectMany
{
// Getting Employee details in EmployeeClass
public class EmployeeClass
{
public int EmployeeID { get; set; }
public string EmployeeName { get; set; }
public List
}
public class DepartmentClass
{
public string Dept_Name { get; set; }
}
class LinqProgram
{
static public void Main()
{
List
employeesList.Add(new EmployeeClass
{
EmployeeID = 1,
EmployeeName = "Smith",
DepartmentList = new List
{
new DepartmentClass { Dept_Name = "Development" },
new DepartmentClass { Dept_Name = "Human-Resources"}
}
});
employeesList.Add(new EmployeeClass
{
EmployeeID = 2,
EmployeeName = "Gorge",
DepartmentList = new List
{
new DepartmentClass { Dept_Name = "Designing" },
new DepartmentClass { Dept_Name = "Business-Analyst"}
}
});
employeesList.Add(new EmployeeClass
{
EmployeeID = 3,
EmployeeName = "Peter",
DepartmentList = new List
{
new DepartmentClass { Dept_Name = "Business-Analyst" },
new DepartmentClass { Dept_Name = "Human-Resources"}
}
});
Console.WriteLine("\nLINQ - SelectMany Method");
Console.WriteLine("-------------------------\n");
Console.WriteLine("Department - Names");
Console.WriteLine("------------------\n");
var getDepartments = employeesList.SelectMany(d => d.DepartmentList, (emp, DepartmentList) => new { emp, DepartmentList });
foreach (var item in getDepartments)
{
Console.WriteLine("\t"+item.emp.EmployeeName + ",\t" + item.DepartmentList.Dept_Name);
}
Console.ReadLine();
}
}
}
}
In this program it follows the third overloaded method as shown in the above syntax, it displays with the reference type it returns the object type which has reference to both department and employee object of each employee.
Output:
Conclusion
In this article, I hope you have learned the SelectMany Operator easily with several examples. By using the SelectMany method we can return a single result from a nested collection. Hope the article helps to understand by seeing the examples programmatically.
Recommended Articles
This is a guide to LINQ SelectMany. Here we discuss the SelectMany Operator easily with several examples along with the programs and outputs. You may also look at the following articles to learn more –
- What is LINQ?
- LINQ Inner Join
- Listbox in C#
- IEnumerable C#
The post LINQ SelectMany appeared first on EDUCBA.
This post first appeared on Free Online CFA Calculator Training Course | EduCB, please read the originial post: here