Select & SelectMany in LINQ, .NET Core Using C#.Net

How to use Select() and SelectMany() method to select the list column values in .net core using c#.net. Get List from a list using c#,.net.
In today's article I will show a simple tutorial with an example how you can use Select and SelectMany in LINQ in .NET core using c#.net. Here I will first explain what Select and SelectMany and will show with the example. This example we can use in console application, class library windows application. Here we will use console application with c#.net. 

Now let's first create a console application and add class file in it. In this we will add the following class property.

 public class EmployeeModel
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Address { get; set; }
    public string Email { get; set; }
} 
In above code I have declared some properties of different data type like int, string. We will make list of this properties. 

Select() In LINQ

Select In LINQ is a method where we will select column values from a list. Here we will use Select () methos and with the lambda expression we will select the columns of the list for which we want to get the value.

Single Column

 List<EmployeeModel> employees = new List<EmployeeModel>() {
     new EmployeeModel { Id=1,Name="John" ,Address="Address10",Email="john@mail.com"},
     new EmployeeModel { Id=2,Name="Mate" ,Address="Address27",Email="mate@mail.com"},
     new EmployeeModel { Id=4,Name="Maru" ,Address="Address10",Email="maru@mail.com"},
     new EmployeeModel { Id=3,Name="Rose" ,Address="Address31",Email="rose@mail.com"},
     new EmployeeModel { Id=5,Name="Krishna" ,Address="Address33",Email="krishna@mail.com"},
     new EmployeeModel { Id=8,Name="Pape" ,Address="Address24",Email="pape@mail.com"},
     new EmployeeModel { Id=10,Name="Pranav" ,Address="Address77",Email="pranav@mail.com"}
};
var data = employees.Select(m => m.Name).ToList();
foreach (var item in data)
{
    Console.WriteLine(string.Format("Name:{0} ", item));
} 
In above code I have added some value in the employee List. To select a specific field from the list, Select() methos is used in c#.net. Now let's run the code to check the output.

Select Column from List Linq

In above code we are able to get only the column "Name" value. Here by using foreach look i have displayed the selected value from the list. Now let's press F5.

Select Method for LINQ in C#.Net

Multiple Column

In this code example i will show you how you can select multiple collum from a list using Select() method.
 List<EmployeeModel> employees = new List<EmployeeModel>() {
     new EmployeeModel { Id=1,Name="John" ,Address="Address10",Email="john@mail.com"},
     new EmployeeModel { Id=2,Name="Mate" ,Address="Address27",Email="mate@mail.com"},
     new EmployeeModel { Id=4,Name="Maru" ,Address="Address10",Email="maru@mail.com"},
     new EmployeeModel { Id=3,Name="Rose" ,Address="Address31",Email="rose@mail.com"},
     new EmployeeModel { Id=5,Name="Krishna" ,Address="Address33",Email="krishna@mail.com"},
     new EmployeeModel { Id=8,Name="Pape" ,Address="Address24",Email="pape@mail.com"},
     new EmployeeModel { Id=10,Name="Pranav" ,Address="Address77",Email="pranav@mail.com"}
};
var data = employees.Select(m => new { m.Name, m.Email }).ToList();
foreach (var item in data)
{
    Console.WriteLine(string.Format("Name:{0}, Email Id:{1}", item.Name,item.Email));
} 
In above code just check the highlighted piece of code. Here I have used new keyword and get the column name with lambada expression.  Now let's run the code and check the output.

Select Multiple Column Linq

Here we are getting the selected column value. Now press F5 and check the output.


Select Multiple Column Linq C#.Net

SelectMany() In LINQ

SelectMany() in LINQ is a method is used for selecting a list available in a list. This act as a group by.  Please refer the below code. 
 List<EmployeeModel> employees = new List<EmployeeModel>() {
     new EmployeeModel { Id=1,Task=new List<string>(){"Task 1","Task 2","Task 3" },Name="John" },
     new EmployeeModel { Id=2,Task=new List<string>(){"Task 1","Task 2" },Name="Mate" ,},
     new EmployeeModel { Id=4,Task=new List<string>(){"Task 3" },Name="Maru" },
     new EmployeeModel { Id=3,Task=new List<string>(){"Task 1","Task 2","Task 3" }},
     new EmployeeModel { Id=5,Task=new List<string>(){"Task 2" },Name="Krishna"},
     new EmployeeModel { Id=8,Task=new List<string>(){"Task 2","Task 3" },Name="Pape"},
     new EmployeeModel { Id=10,Task=new List<string>(){"Task 1","Task 3" },Name="Pranav" }
};
var data = employees.SelectMany(m => m.Task).ToList();
foreach (var item in data)
{
    Console.WriteLine(string.Format("Task:{0}", item));
} 
In above code you can see that in Employee List there is a list named as Task. Now we will use SelectMany() to get the list of all tasks. To get the deistic value we can use Distinct() method to remove the duplicate values.  Now we have done run the code to check the output.

List in a List

Here we are able to get the list of all task. Here is final output.

SelectMany

Post a Comment