What is SelectMany

SelectMany projects each elements result into one sequence i.e IEnumerable<T>.

Earlier we used select operator to select data from linq.

Example of SelectMany

Take a class in which one filed is another data source like array, list etc. as I have created contact number as int array.

	public class employee
    public int id { get; set; }
    public string name { get; set; }
    public string dept { get; set; }
    public Int64[] contact { get; set; }


Now I have created list to add data

		List<employee> emp = new List<employee>()
            new employee(){ id=101, name="isha", dept="hr", contact=new Int64[]{9787878787, 8989898980}},
            new employee(){ id=102, name="neha", dept="sales", contact=new Int64[]{9786868687, 8989898986}},
            new employee(){ id=103, name="pooja", dept="hr", contact=new Int64[]{9787888087, 8989898984}},
            new employee(){ id=104, name="rahul", dept="development", contact=new Int64[]{1234567890, 8989898987}}


Now I need to fetch the contact numbers which is stored in the format of array.

So result set will be like IEnumerable<Int64[]>

I need to use two loop to traverse this result.

Response.Write("Result of Select Operator <br>");
       IEnumerable<Int64[]> res= emp.Select(x => x.contact);
       foreach (Int64[] data in res)

           foreach (Int64 contact in data)

               Response.Write(contact + "<br>");


Now use selectMany operator in which it projects all elements into single sequence

Response.Write("Result of Select Many <br>");
IEnumerable<Int64> res_selectmany = emp.SelectMany(x => x.contact);
foreach (Int64 contact in res_selectmany)

           Response.Write(contact + "<br>");



Now execute the code

select many in linq
		Figure 2


Email Address

For any query you can send mail at info@techaltum.com