Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

LINQ SelectMany

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.

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 and then it finally returns the flat result as one single sequence. To understand clearly the operator works when we having the collection of properties from that we need to specify each and every item to collection one at a time.

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 DepartmentList { get; set; }
}
public class DepartmentClass
{
public string Dept_Name { get; set; }
}
class LinqProgram
{
static public void Main()
{
List employeesList = new 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 DepartmentList { get; set; }
}
public class DepartmentClass
{
public string Dept_Name { get; set; }
}
class LinqProgram
{
static public void Main()
{
List employeesList = new 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 DepartmentList { get; set; }
}
public class DepartmentClass
{
public string Dept_Name { get; set; }
}
class LinqProgram
{
static public void Main()
{
List employeesList = new 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 –

  1. What is LINQ?
  2. LINQ Inner Join
  3. Listbox in C#
  4. 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

Share the post

LINQ SelectMany

×

Subscribe to Free Online Cfa Calculator Training Course | Educb

Get updates delivered right to your inbox!

Thank you for your subscription

×