Skip to content

Bits of .NET

Daily micro-tips for C#, SQL, performance, and scalable backend engineering.

  • Asp.Net Core
  • C#
  • SQL
  • JavaScript
  • CSS
  • About
  • ErcanOPAK.com
  • No Access
C# / LINQ

What is the difference between Select and SelectMany in Linq

- 30.12.22 - ErcanOPAK

The formal description for SelectMany() is:

Projects each element of a sequence to an IEnumerable and flattens the resulting sequences into one sequence.

SelectMany() flattens the resulting sequences into one sequence and invokes a result selector function on each element.

enter image description here

The main difference is the result of each method while SelectMany() returns flattened results; Select() returns a list of lists instead of a flatter result set.

Therefore the result of SelectMany is a list in which you can iterate each item by just one foreach. But with the result of select you need an extra foreach loop to iterate through the results because the query returns a collection of arrays.

Let us create a class file with the name Student.cs and then copy and paste the following code.

using System.Collections.Generic;
namespace LINQDemo
{
    public class Student
    {
        public int ID { get; set; }
        public string Name { get; set; }
        public string Email { get; set; }
        public List<string> Programming { get; set; }

        public static List<Student> GetStudents()
        {
            return new List<Student>()
            {
                new Student(){ID = 1, Name = "James", Email = "James@j.com", Programming = new List<string>() { "C#", "Jave", "C++"} },
                new Student(){ID = 2, Name = "Sam", Email = "Sara@j.com", Programming = new List<string>() { "WCF", "SQL Server", "C#" }},
                new Student(){ID = 3, Name = "Patrik", Email = "Patrik@j.com", Programming = new List<string>() { "MVC", "Jave", "LINQ"} },
                new Student(){ID = 4, Name = "Sara", Email = "Sara@j.com", Programming = new List<string>() { "ADO.NET", "C#", "LINQ" } }
            };
        }
    }
}

As you can see we have created the Student class with four properties. Please remember the Programming property returns list<string>. Here we also created one method which will return the List of students which will be going to act our data source.

We need to Projects all programming strings of all the students to a single IEnumerable<string>. As you can see, we have 4 students, so there will be 4 IEnumerable<string> sequences, which then we need to flattened to form a single sequence i.e. a single IEnumerable<string> sequence.

SelectMany in LINQ
If you want only the distinct program names and to retrieve the student name along with them then you should use that:
using System;
using System.Collections.Generic;
using System.Linq;

namespace LINQDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            //Using Method Syntax
            var MethodSyntax = Student.GetStudents()
                                        .SelectMany(std => std.Programming,
                                             (student, program) => new
                                             {
                                                 StudentName = student.Name,
                                                 ProgramName = program
                                             }
                                             )
                                        .ToList();

            //Using Query Syntax
            var QuerySyntax = (from std in Student.GetStudents()
                               from program in std.Programming
                               select new {
                                   StudentName = std.Name,
                                   ProgramName = program
                               }).ToList();

            //Printing the values
            foreach (var item in QuerySyntax)
            {
                Console.WriteLine(item.StudentName + " => " + item.ProgramName);
            }

            Console.ReadKey();
        }
    }
}

Output:

Thanks for a such enlightening article.

Related posts:

ASP.NET Core Requests Hang Under Load

How to run code if not in Debug Mode in C#

How to Validate a DateTime in C#?

Post Views: 55

Post navigation

Differences between FirstOrDefault and SingleOrDefault in LINQ
What is the purpose of nameof in C#?

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

January 2026
M T W T F S S
 1234
567891011
12131415161718
19202122232425
262728293031  
« Dec    

Most Viewed Posts

  • Get the User Name and Domain Name from an Email Address in SQL (924)
  • How to add default value for Entity Framework migrations for DateTime and Bool (820)
  • Get the First and Last Word from a String or Sentence in SQL (816)
  • How to select distinct rows in a datatable in C# (790)
  • How to make theater mode the default for Youtube (687)
  • Add Constraint to SQL Table to ensure email contains @ (565)
  • How to enable, disable and check if Service Broker is enabled on a database in SQL Server (545)
  • Average of all values in a column that are not zero in SQL (512)
  • How to use Map Mode for Vertical Scroll Mode in Visual Studio (467)
  • Find numbers with more than two decimal places in SQL (433)

Recent Posts

  • Why foreach Is Sometimes Slower Than for
  • The Hidden Cost of Exceptions as Flow Control
  • Why lock Can Kill Throughput
  • Indexes Can Make Queries Slower (Here’s Why)
  • Why SELECT * Slowly Destroys Performance
  • The Real Cost of IHostedService Misuse
  • Why Async Controllers Still Block Threads
  • Stop Using git pull (Yes, Really)
  • Why Git Rebase “Loses” Commits (It Doesn’t — You Did)
  • Why Fetch Requests “Randomly” Hang (But Server Is Fine)

Most Viewed Posts

  • Get the User Name and Domain Name from an Email Address in SQL (924)
  • How to add default value for Entity Framework migrations for DateTime and Bool (820)
  • Get the First and Last Word from a String or Sentence in SQL (816)
  • How to select distinct rows in a datatable in C# (790)
  • How to make theater mode the default for Youtube (687)

Recent Posts

  • Why foreach Is Sometimes Slower Than for
  • The Hidden Cost of Exceptions as Flow Control
  • Why lock Can Kill Throughput
  • Indexes Can Make Queries Slower (Here’s Why)
  • Why SELECT * Slowly Destroys Performance

Social

  • ErcanOPAK.com
  • GoodReads
  • LetterBoxD
  • Linkedin
  • The Blog
  • Twitter
© 2026 Bits of .NET | Built with Xblog Plus free WordPress theme by wpthemespace.com