Hello readers, In this tutorial, we are going to understand what is SelectMany Operator in LINQ with the example. SelectMany let you collapse a multidimensional sequence in a way that it would not require a second select() operator or loop.

What is SelectMany Operator in LINQ C#:

The SelectMany operator comes under the projection operators category of Linq Query Operator.  It is just like the Cross Join in SQL Server.

For Example if we have two set of array as:

Set ArrayA={a,b,c};

Set ArrayB={x,y};

Then SelectMany can produce this type of result.

{ (x,a) , (x,b) , (x,c) , (y,a) , (y,b) , (y,c) }

SelectMany let you collapse a multidimensional sequence in a way that it would not require a second select() operator or loop.

Scenario:

Suppose you have a sequence of Candidate object where each candidate is having some set of skills and you need to enumerate each skill one by one.

Example1:

Let’s create an example using the scenario what we have discussed.

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using System.IO;

using System.Runtime.Serialization.Json;

namespace SelectManyDemo

{




    class Candidate

    {

        public string Name { get; set; }

        public List<string> Skills { get; set; }

    }

    class Program

    {




        public static List<Candidate> GetCandidate()

        {

            List<Candidate> lstCandidate = new List<Candidate>();

            Candidate objCandidate1 = new Candidate { Name = "Vikas", Skills = new List<string> { "C", "C++", "Java" } };

            Candidate objCandiate2 = new Candidate { Name = "Ashutosh", Skills = new List<string> { "SQL Server", "C#", "ASP.NET","Salesforce" } };

            Candidate objCandidate3 = new Candidate { Name = "Arun", Skills = new List<string> { "C#", "ASP.NET MVC", "Windows Azure", "SQL Server" } };

            Candidate objCandidate4 = new Candidate { Name = "PAnkaj", Skills = new List<string> { "C#", "ASP.NET MVC", "Windows Azure", "SQL Server","Salesforce","Android" } };

            lstCandidate.Add(objCandidate1);

            lstCandidate.Add(objCandiate2);

            lstCandidate.Add(objCandidate3);

            lstCandidate.Add(objCandidate4);

            return lstCandidate;

        }

        static void Main(string[] args)

        {

            List<Candidate> lstCandidate = GetCandidate();

            IEnumerable<string> lstSkill = lstCandidate.SelectMany(e => e.Skills);

            foreach (string sSkill in lstSkill)

            {

                Console.WriteLine(sSkill);

            }

            Console.ReadLine();       

        }       

    }

}

Output:

SelectMany

In the above example, we have created a Candidate class which has two properties Name and Skills. We have added four candidates in candidate collection with the different skill set. Now the SelectMany operator returns the skills from the child collection of candidate collection with using the another select() operator or loop.

   IEnumerable<string> lstSkill = lstCandidate.SelectMany(e => e.Skills);

            foreach (string sSkill in lstSkill)

            {

                Console.WriteLine(sSkill);

            }

Exmple2 :

As we already discussed that the SelectMany converts the two-dimensional array into a single sequence of value. Here I am going to show how it works.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Text;
using System.Runtime.Serialization.Json;
namespace SelectManyDemo
{


class Program
{

static void Main(string[] args)
{
int[][] sequence = {
new[] {1, 2, 3},
new[] {4,5},
new[] { 6, 7, 8,9},
new[] {12, 14}
};
// Will return { 1, 2, 3, 4, 5, 6, 7, 8, 12, 14 }
IEnumerable<int> result = sequence.SelectMany(array => array);
foreach (int iValue in result)
{
Console.WriteLine(iValue);
}
Console.ReadLine();
 
}
 
}
}

Output:

SelectMany

View More:

View More:

I hope you would love this post. Please feel free to comment for any technical help. Your feedback and suggestions are always appreciated.

Thank You.

Leave a Comment