Difference between Select and SelectMany in LINQ

Difference between Select and SelectMany in LINQ

29 Mar 2024
Intermediate
53.8K Views
6 min read
Learn with an interactive course and practical hands-on labs

ASP.NET MVC with WebAPI Course

Select Vs Select-Many in LINQ: An Overview

In this LINQ Tutorial, you will learn SELECT and SELECT-MANY Operators in LINQ and select and select many Operators with some actual Programming Examples.

Select and Select-Many, both are projection operators, which means, they select values from the list, collection, or other sources. A select operator is used to select values from a collection while a Select-Many operator is used to select values from a collection of collections i.e. nested collection. Select operator produces one result value for every source value while SelectMany produces a single result that contains a concatenated value for every source value. And SelectMany operator flattens IEnumerable<IEnumerable<T>> to IEnumrable<T> i.e. list of list to list.

You can understand Select and SelectMany Operator in LINQ more clearly when you see the actual programming examples.

SELECT OPERATOR EXAMPLE:

using System;
using System.Collections.Generic;
using System.Linq;
 
namespace OperatorTutorial
{
    class SoftwareCompany
    {
        public string employeeName { get; set; }
        public int employeeSalary { get; set; }
        public List employeeDetails { get; set; }
    }
 
    class Program
    {
        static void Main(string[] args)
        {
            var result = from p in GetCompanyDetails()
                         select new { p.employeeName, p.employeeSalary, p.employeeDetails };
            foreach (var r in result)
            {
                Console.WriteLine(r);
            }
            Console.ReadKey();
        }
 
        //Creating List of employee
        static List GetCompanyDetails()
        {
            List employee = new List
            {
            new SoftwareCompany
                {
               employeeName = "Vishnu",
               employeeSalary = 20000,
               employeeDetails = new List{"Software developer","Pune","Maharashtra"}
                },
            new SoftwareCompany
                {
               employeeName = "Priya",
               employeeSalary = 40000,
               employeeDetails = new List{"QA","Mumbai","Maharashtra"}
                },
            new SoftwareCompany
                {
               employeeName = "Jagat",
               employeeSalary = 800000,
               employeeDetails = new List{"Manager","Banglore","Karnataka "}
                },
            };
            return employee;
        }
    }
}

Output:

{ employeeName = Vishnu, employeeSalary = 20000, employeeDetails = System.Collections.Generic.List`1[System.String] }
{ employeeName = Priya, employeeSalary = 40000, employeeDetails = System.Collections.Generic.List`1[System.String] }
{ employeeName = Jagat, employeeSalary = 800000, employeeDetails = System.Collections.Generic.List`1[System.String] }

Explanation:

If you read the output carefully, you will notice that the "employeeDetails" is not displayed correctly. At the place of "employeeDetails", the program displays the "System.Collections.Generic.List" message. This is just because employeeDetails is a nested list and to overcome this problem to display the field properly, we need to implement SelectMany Operator.

SELECT MANY OPERATOR EXAMPLE:

using System;
using System.Collections.Generic;
using System.Linq;
 
namespace OperatorTutorial
{
    class SoftwareCompany
    {
        public string employeeName { get; set; }
        public int employeeSalary { get; set; }
        public List employeeDetails { get; set; }
    }
 
    class Program
    {
        static void Main(string[] args)
        {
            var result = from p in GetCompanyDetails()
                         select new { p.employeeName, p.employeeSalary, p.employeeDetails };;
                         
            foreach (var r in result.SelectMany(SoftwareCompany => SoftwareCompany.employeeDetails))
            {
                Console.WriteLine(r);
            }
            Console.ReadKey();
        }
 
        //Creating List of employee
        static List GetCompanyDetails()
        {
            List employee = new List
            {
            new SoftwareCompany
                {
               employeeName = "Vishnu",
               employeeSalary = 20000,
               employeeDetails = new List{"Software developer","Pune","Maharashtra"}
                },
            new SoftwareCompany
                {
               employeeName = "Priya",
               employeeSalary = 40000,
               employeeDetails = new List{"QA","Mumbai","Maharashtra"}
                },
            new SoftwareCompany
                {
               employeeName = "Jagat",
               employeeSalary = 800000,
               employeeDetails = new List{"Manager","Banglore","Karnataka "}
                },
            };
            return employee;
        }
    }
}

Output:

Output:

Software developer
Pune
Maharashtra
QA
Mumbai
Maharashtra
Manager
Banglore
Karnataka 

Explanation:

In the above example, We have used Select Many Operator to print nested list values. So according to it, select-many operator is useful when working with collections of collections (e.g., lists of lists or arrays of arrays) or when you want to transform and combine elements from multiple sources into a single sequence.
Summary:

I hope you will enjoy Select and SelectMany while programming with LINQ. I would like to have feedback from my blog readers. Your valuable feedback, questions, or comments about this article are always welcome. Read more articles related to LINQ. Enjoy coding...!

FAQs

when you want to work with collections of collections or when you need to flatten and project data from nested structures

The use of FirstOrDefault<TSource>(IEnumerable<TSource>, Func<TSource,Boolean>, TSource) Returns the first element of the sequence that satisfies a condition, or a specified default value if no such element is found.

To select multiple values from a list using LINQ, we can use the Select method. This method allows you to project each element of a sequence into a new form.
Share Article
About Author
Shailendra Chauhan (Microsoft MVP, Founder & CEO at Scholarhat by DotNetTricks)

Shailendra Chauhan, Founder and CEO of ScholarHat by DotNetTricks, is a renowned expert in System Design, Software Architecture, Azure Cloud, .NET, Angular, React, Node.js, Microservices, DevOps, and Cross-Platform Mobile App Development. His skill set extends into emerging fields like Data Science, Python, Azure AI/ML, and Generative AI, making him a well-rounded expert who bridges traditional development frameworks with cutting-edge advancements. Recognized as a Microsoft Most Valuable Professional (MVP) for an impressive 9 consecutive years (2016–2024), he has consistently demonstrated excellence in delivering impactful solutions and inspiring learners.

Shailendra’s unique, hands-on training programs and bestselling books have empowered thousands of professionals to excel in their careers and crack tough interviews. A visionary leader, he continues to revolutionize technology education with his innovative approach.
Accept cookies & close this