23
NovDifference between Select and SelectMany in LINQ
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:
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:
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...!