LINQ Select Operator in C
LINQ Select Operator in C
Note: In the Query Syntax, the data type of the basicPropQuery is List<int>, this is because
of the ToList() method that we applied on the query syntax. And because of this ToList()
method, the query is executed at that point only.
But in the case of Method syntax, we have not applied the ToList() method and this is the
reason why the data type of the basicPropMethod variable is of IEnumerable<int> type. And
more importantly, at that point, the query is just generated but not executed. When we use
the basicPropMethod within the foreach loop, then at that time the query is executed.
The complete example is given below.
namespace LINQDemo
{
class Program
{
static void Main(string[] args)
{
//Using Query Syntax
List<int> basicPropQuery = (from emp in Employee.GetEmployees()
select emp.ID)
.ToList();
foreach (var id in basicPropQuery)
{
Console.WriteLine($"ID : {id}");
}
//Using Method Syntax
IEnumerable<int> basicPropMethod = Employee.GetEmployees()
.Select(emp => emp.ID);
foreach (var id in basicPropMethod)
{
Console.WriteLine($"ID : {id}");
}
Console.ReadKey();
}
}
}
Example3:
Our requirement is to select only the Employee First Name, Last Name, and Salary
properties. We don’t require to select the ID property.
The Complete code is given below.
using System;
using System.Collections.Generic;
using System.Linq;
namespace LINQDemo
{
class Program
{
static void Main(string[] args)
{
//Query Syntax
IEnumerable<Employee> selectQuery = (from emp in
Employee.GetEmployees()
select new Employee()
{
FirstName = emp.FirstName,
LastName = emp.LastName,
Salary = emp.Salary
});
foreach (var emp in selectQuery)
{
Console.WriteLine($" Name : {emp.FirstName} {emp.LastName} Salary :
{emp.Salary} ");
}
//Method Syntax
List<Employee> selectMethod = Employee.GetEmployees().
Select(emp => new Employee()
{
FirstName = emp.FirstName,
LastName = emp.LastName,
Salary = emp.Salary
}).ToList();
foreach (var emp in selectMethod)
{
Console.WriteLine($" Name : {emp.FirstName} {emp.LastName} Salary :
{emp.Salary} ");
}
Console.ReadKey();
}
}
}
How to Select Data to another class using LINQ Projection Operator?
It is also possible to select the data to another class using the LINQ Select operator. In our
previous example, we have selected the First Name, Last Name, and Salary properties to
the same Employee class. Let us create a new class with the above three properties. So,
add a new class file with the name EmployeeBasicInfo.cs and then copy and paste the
following code.
namespace LINQDemo
{
public class EmployeeBasicInfo
{
public string FirstName { get; set; }
public string LastName { get; set; }
public int Salary { get; set; }
}
}
Now what we need to do here is, we need to project the First Name, Last Name, and Salary
properties to the above newly created EmployeeBasicInfo class.
class Program
employees.Add(emp1);
employees.Add(emp2);
employees.Add(emp3);
// Query using Select()
IEnumerable<List<String>> resultSelect =
employees.Select(e=> e.Skills);
Console.WriteLine("**************** Select
******************");
Console.WriteLine("****************
SelectMany ******************");
Console.WriteLine(skill);
Console.ReadKey();
/* Output
**************** Select ******************
C++
Java
SQL Server
C#
ASP.NET
C#
ASP.NET MVC
Windows Azure
SQL Server
C++
Java
SQL Server
C#
ASP.NET
C#
Single
It returns a single specific element from a collection of elements if element
match found. An exception is thrown, if none or more than one match found
for that element in the collection.
SingleOrDefault
It returns a single specific element from a collection of elements if element
match found. An exception is thrown, if more than one match found
for that element in the collection. A default value is returned, if no
match is found for that element in the collection.
List<int> data = new List<int> { 10, 20, 30, 40, 50 };
Console.WriteLine(data.ElementAt(1)); //result:20
Console.WriteLine(data.First()); //result:10
Console.WriteLine(data.Last()); //result:50
//try to get first element from matching elements collection
//try to get first element from matching elements collection else returns
default value
//try to get single element 100 if exist otherwise returns default value
First
It returns first specific element from a collection of elements if one or more
than one match found for that element. An exception is thrown, if no match
is found for that element in the collection.
FirstOrDefault
It returns first specific element from a collection of elements if one or more
than one match found for that element. A default value is returned, if no
match is found for that element in the collection.