Web
Analytics
SelectMany query operator in linq | Angular | ASP.NET Tutorials

For Consultation : +91 9887575540

Stay Connected :

SelectMany query operator produces a variable number of output elements for each input element.

In simple words if any collection have collection as an its element then using selectmany we can read that child connection item.

 Example

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LINQExample
{
class Program
{
static void Main(string[] args)
{
employeedetails[] emp =
{
new employeedetails{ employeename="Ganesha", department="IT", age=30, emptraining= new List<string>{ "php","asp"}},
new employeedetails{ employeename="Yogesh", department="IT", age=20, emptraining= new List<string>{ "ruby","oracle"}},
new employeedetails{ employeename="Ashish", department="Sales", age=10, emptraining= new List<string>{ "php","oracle"}},
new employeedetails{ employeename="Pooja", department="Teaching", age=15, emptraining= new List<string>{ "java","asp"}},
new employeedetails{ employeename="Arti", department="Teaching", age=40, emptraining= new List<string>{ "java","oracle"}},
};

IEnumerable<string> trainings = emp.SelectMany(x => x.emptraining);
foreach (string emptrained in trainings)
{
Console.WriteLine("EmployeeTrainingsAre =" + emptrained);
}
Console.ReadLine();
}
}

public class employeedetails
{
public string employeename;
public string department;
public int age;
public List<string> emptraining;
}
}

output

LINQ_SelectMany_Projection_Example_Output

 

Download Source Code