Introduction
In this article, we will cover some of the important methods of LINQ used in our development.
So, Let's get started.
What is LINQ
LINQ stands for Language-Integrated Query, and it is a powerful query language that was introduced with .NET 3.5 & Visual Studio 2008. You can use LINQ with C# or VB to query different types of data sources such as SQL, XML, In memory objects, etc. LINQ provides a simple but very effective way to manipulate data from different data sources such as databases, XML files, or with a simple list of in-memory data.
Note. To use Linq, we need to use System.Linq namespace.
LINQ Methods
Select(): It is used to format the result of the query as per our requirement.
Example
using System;
using System.Collections.Generic;
using System.Linq;
public class Program
{
public class Employee
{
public int ID { get; set; }
public string Name { get; set; }
public int Salary { get; set; }
public string Department { get; set; }
public static List<Employee> GetAllEmployees()
{
List<Employee> listEmployees = new List<Employee>()
{
new Employee{ID= 101,Name = "Pooja", Salary = 10000, Department = "IT"},
new Employee{ID= 102,Name = "Priyanka", Salary = 15000, Department = "Sales"},
new Employee{ID= 103,Name = "Manoj", Salary = 25000, Department = "Sales"},
new Employee{ID= 104,Name = "Santosh", Salary = 20000, Department = "IT"},
new Employee{ID= 105,Name = "Vishal", Salary = 30000, Department = "IT"},
new Employee{ID= 106,Name = "Sandhya", Salary = 25000, Department = "IT"},
new Employee{ID= 107,Name = "Mahesh", Salary = 35000, Department = "IT"},
new Employee{ID= 108,Name = "Manoj", Salary = 11000, Department = "Sales"},
new Employee{ID= 109,Name = "Pradeep", Salary = 20000, Department = "Sales"},
new Employee{ID= 110,Name = "Saurav", Salary = 25000, Department = "Sales"}
};
return listEmployees;
}
}
public static void Main()
{
//Using Method Syntax
var MethodSyntax = Employee.GetAllEmployees().ToList();
//Using Query Syntax
var QuerySyntax = (from emp in Employee.GetAllEmployees()
select emp).ToList();
foreach (var emp in MethodSyntax)
{
Console.WriteLine($"ID : {emp.ID} Name : {emp.Name} {emp.Salary}");
}
foreach (var emp in QuerySyntax)
{
Console.WriteLine($"ID : {emp.ID} Name : {emp.Name} {emp.Salary}");
}
Console.ReadKey();
}
}
Where(): It is used to Filter a sequence on a based condition.
Example 1
using System;
using System.Linq;
public class Program
{
public static void Main()
{
int[] IntArray = { 10, 20, 30, 40, 50, 60 };
//Using Method Syntax
var MethodSyntax = IntArray.Where(whr=> whr > 30);
//Using Query Syntax
var QuerySyntax = (from num in IntArray where num > 30 select num);
foreach (var item in MethodSyntax )
{
Console.WriteLine(item);
}
foreach (var item in QuerySyntax )
{
Console.WriteLine(item);
}
Console.ReadKey();
}
}
Example 2
using System;
using System.Collections.Generic;
using System.Linq;
public class Program
{
public class Employee
{
public int ID { get; set; }
public string Name { get; set; }
public int Salary { get; set; }
public string Department { get; set; }
public static List<Employee> GetAllEmployees()
{
List<Employee> listEmployees = new List<Employee>()
{
new Employee{ID= 101,Name = "Pooja", Salary = 10000, Department = "IT"},
new Employee{ID= 102,Name = "Priyanka", Salary = 15000, Department = "Sales"},
new Employee{ID= 103,Name = "Manoj", Salary = 25000, Department = "Sales"},
new Employee{ID= 104,Name = "Santosh", Salary = 20000, Department = "IT"},
new Employee{ID= 105,Name = "Vishal", Salary = 30000, Department = "IT"},
new Employee{ID= 106,Name = "Sandhya", Salary = 25000, Department = "IT"},
new Employee{ID= 107,Name = "Mahesh", Salary = 35000, Department = "IT"},
new Employee{ID= 108,Name = "Manoj", Salary = 11000, Department = "Sales"},
new Employee{ID= 109,Name = "Pradeep", Salary = 20000, Department = "Sales"},
new Employee{ID= 110,Name = "Saurav", Salary = 25000, Department = "Sales"}
};
return listEmployees;
}
}
public static void Main()
{
//Using Method Syntax
var MethodSyntax = Employee.GetAllEmployees().Where(whr=> whr.Department == "IT").ToList();
//Using Query Syntax
var QuerySyntax = (from emp in Employee.GetAllEmployees() where emp.Department == "Sales" select emp).ToList();
foreach (var emp in MethodSyntax)
{
Console.WriteLine($"ID : {emp.ID} Name : {emp.Name} {emp.Salary}");
}
foreach (var emp in QuerySyntax)
{
Console.WriteLine($"ID : {emp.ID} Name : {emp.Name} {emp.Salary}");
}
Console.ReadKey();
}
}
Distinct(): It is used to remove the duplicate elements from a sequence (list) and returns the distinct elements from a single data source.
Example 1
using System;
using System.Linq;
public class Program
{
public static void Main()
{
int[] IntArray = { 10, 20, 20, 40, 60, 60, 70 };
//Using Method Syntax
var MethodSyntax = IntArray.Distinct();
//Using Query Syntax
var QuerySyntax = (from num in IntArray select num).Distinct();
foreach (var item in MethodSyntax )
{
Console.WriteLine(item);
}
foreach (var item in QuerySyntax )
{
Console.WriteLine(item);
}
Console.ReadKey();
}
}
Example 2
using System;
using System.Collections.Generic;
using System.Linq;
public class Program
{
public class Employee
{
public int ID { get; set; }
public string Name { get; set; }
public int Salary { get; set; }
public string Department { get; set; }
public static List<Employee> GetAllEmployees()
{
List<Employee> listEmployees = new List<Employee>()
{
new Employee{ID= 101,Name = "Pooja", Salary = 15000, Department = "IT"},
new Employee{ID= 102,Name = "Priyanka", Salary = 15000, Department = "Sales"},
new Employee{ID= 103,Name = "Manoj", Salary = 25000, Department = "Sales"},
new Employee{ID= 104,Name = "Santosh", Salary = 20000, Department = "IT"},
new Employee{ID= 105,Name = "Vishal", Salary = 30000, Department = "IT"},
new Employee{ID= 106,Name = "Sandhya", Salary = 25000, Department = "IT"},
new Employee{ID= 107,Name = "Mahesh", Salary = 35000, Department = "IT"},
new Employee{ID= 108,Name = "Manoj", Salary = 11000, Department = "Sales"},
new Employee{ID= 109,Name = "Pradeep", Salary = 20000, Department = "Sales"},
new Employee{ID= 110,Name = "Saurav", Salary = 25000, Department = "Sales"}
};
return listEmployees;
}
}
public static void Main()
{
//Using Method Syntax
var MethodSyntax = Employee.GetAllEmployees().Select(x => x.Name).Distinct().ToList();
//Using Query Syntax
var QuerySyntax = (from emp in Employee.GetAllEmployees() select emp.Name).Distinct().ToList();
foreach (var item in MethodSyntax )
{
Console.WriteLine(item);
}
foreach (var item in QuerySyntax )
{
Console.WriteLine(item);
}
Console.ReadKey();
}
}
ElementAt(): It is used to return the element at a specified index in a sequence.
Example
using System;
using System.Linq;
public class Program
{
public static void Main()
{
int[] IntArray = { 10, 20, 30, 40, 50, 60, 70 };
//Using Method Syntax
var MethodSyntax = IntArray.ElementAt(3);
//Using Query Syntax
var QuerySyntax = (from num in IntArray select num).ElementAt(4);
Console.WriteLine(MethodSyntax);
Console.WriteLine(QuerySyntax);
Console.ReadKey();
}
}
ElementAtOrDefault(): This method is used to result out a specific element from the respective collection by specifying the specific index.
using System;
using System.Linq;
public class Program
{
public static void Main()
{
int[] IntArray = { 10, 20, 30, 40, 50, 60, 70 };
//Using Method Syntax
var MethodSyntax = IntArray.ElementAtOrDefault(-1);
//Using Query Syntax
var QuerySyntax = (from num in IntArray select num).ElementAtOrDefault(2);
Console.WriteLine(MethodSyntax);
Console.WriteLine(QuerySyntax);
Console.ReadKey();
}
}
Empty(): It is used to return an empty collection (i.e. IEnumerable) of a specified type.
Example 1: Checking Null Before using inside the Loop.
using System;
using System.Collections.Generic;
using System.Linq;
public class Program
{
public class Employee
{
public int ID { get; set; }
public string Name { get; set; }
public int Salary { get; set; }
public string Department { get; set; }
public static List<Employee> GetAllEmployees()
{
List<Employee> listEmployees = new List<Employee>()
{
new Employee{ID= 101,Name = "Pooja", Salary = 15000, Department = "IT"},
new Employee{ID= 102,Name = "Priyanka", Salary = 15000, Department = "Sales"},
new Employee{ID= 103,Name = "Manoj", Salary = 25000, Department = "Sales"},
new Employee{ID= 104,Name = "Santosh", Salary = 20000, Department = "IT"},
new Employee{ID= 105,Name = "Vishal", Salary = 30000, Department = "IT"},
new Employee{ID= 106,Name = "Sandhya", Salary = 25000, Department = "IT"},
new Employee{ID= 107,Name = "Mahesh", Salary = 35000, Department = "IT"},
new Employee{ID= 108,Name = "Manoj", Salary = 11000, Department = "Sales"},
new Employee{ID= 109,Name = "Pradeep", Salary = 20000, Department = "Sales"},
new Employee{ID= 110,Name = "Saurav", Salary = 25000, Department = "Sales"}
};
return listEmployees;
}
}
public static void Main()
{
var Result = Employee.GetAllEmployees();
if(Result != null)
{
foreach (var item in Result )
{
Console.WriteLine($"Name : {item.Name}");
}
}
Console.ReadKey();
}
}
Example 2: Using NULL-COALESCING
using System;
using System.Collections.Generic;
using System.Linq;
public class Program
{
public class Employee
{
public int ID { get; set; }
public string Name { get; set; }
public int Salary { get; set; }
public string Department { get; set; }
public static List<Employee> GetAllEmployees()
{
List<Employee> listEmployees = new List<Employee>()
{
new Employee{ID= 101,Name = "Pooja", Salary = 15000, Department = "IT"},
new Employee{ID= 102,Name = "Priyanka", Salary = 15000, Department = "Sales"},
new Employee{ID= 103,Name = "Manoj", Salary = 25000, Department = "Sales"},
new Employee{ID= 104,Name = "Santosh", Salary = 20000, Department = "IT"},
new Employee{ID= 105,Name = "Vishal", Salary = 30000, Department = "IT"},
new Employee{ID= 106,Name = "Sandhya", Salary = 25000, Department = "IT"},
new Employee{ID= 107,Name = "Mahesh", Salary = 35000, Department = "IT"},
new Employee{ID= 108,Name = "Manoj", Salary = 11000, Department = "Sales"},
new Employee{ID= 109,Name = "Pradeep", Salary = 20000, Department = "Sales"},
new Employee{ID= 110,Name = "Saurav", Salary = 25000, Department = "Sales"}
};
return listEmployees;
}
}
public static void Main()
{
var Result = Employee.GetAllEmployees() ?? Enumerable.Empty<Employee>();
if(Result != null)
{
foreach (var item in Result )
{
Console.WriteLine($"Name : {item.Name}");
}
}
Console.ReadKey();
}
}
DefaultIfEmpty(): It is used to handle empty collections.
Example 1
using System;
using System.Linq;
public class Program
{
public static void Main()
{
int[] IntArray = { 10, 20, 30, 40, 50, 60, 70 };
//DefaultIfEmpty Method will return a new sequence with existing sequence values
//Using Method Syntax
var MethodSyntax = IntArray.DefaultIfEmpty();
//Using Query Syntax
var QuerySyntax = (from num in IntArray select num).DefaultIfEmpty();
foreach (var num in MethodSyntax)
{
Console.Write($"{num} ");
}
foreach (var num in QuerySyntax)
{
Console.Write($"{num} ");
}
Console.ReadKey();
}
}
Example 2
using System;
using System.Linq;
public class Program
{
public static void Main()
{
//Sequence is empty
var IntArray = new List<int>();
//DefaultIfEmpty Method will return a new sequence with existing sequence values
//Using Method Syntax
var MethodSyntax = IntArray.DefaultIfEmpty();
//Using Query Syntax
var QuerySyntax = (from num in IntArray select num).DefaultIfEmpty();
foreach (var num in MethodSyntax)
{
Console.Write($"{num} ");
}
foreach (var num in QuerySyntax)
{
Console.Write($"{num} ");
}
Console.ReadKey();
}
}
Example 3
using System;
using System.Linq;
public class Program
{
public static void Main()
{
//Sequence is empty
var IntArray = new List<int>();
//DefaultIfEmpty Method will return a new sequence with existing sequence values
//Using Method Syntax
var MethodSyntax = IntArray.DefaultIfEmpty(4);
//Using Query Syntax
var QuerySyntax = (from num in IntArray select num).DefaultIfEmpty(2);
foreach (var num in MethodSyntax)
{
Console.Write($"{num} ");
}
foreach (var num in QuerySyntax)
{
Console.Write($"{num} ");
}
Console.ReadKey();
}
}
Contains(): It is used to check whether a sequence or collection (i.e. data source) contains a specified element or not.
Example 1
using System;
using System.Linq;
public class Program
{
public static void Main()
{
int[] IntArray = { 10, 20, 30, 40, 50, 60 };
//Using Method Syntax
var MethodSyntax = IntArray.Contains(30);
//Using Query Syntax
var QuerySyntax = (from num in IntArray select num).Contains(30);
Console.WriteLine($"Is Element 30 Exist: {MethodSyntax }");
Console.WriteLine($"Is Element 30 Exist: {QuerySyntax }");
Console.ReadKey();
}
}
Example 2
using System;
using System.Linq;
public class Program
{
public static void Main()
{
var namesList = new string[] { "vishal", "sachin", "sourav", "mahesh", "santosh" };
//Using Method Syntax
var MethodSyntax = namesList.Contains("vishal");
//Using Query Syntax
var QuerySyntax = (from name in namesList select name).Contains("vishal");
Console.WriteLine($"Name Exist: {Syntax}");
Console.WriteLine($"Name Exist: {QuerySyntax}");
Console.ReadKey();
}
}
Example 3: This example uses the query and the method syntax to find all words containing the 'v' character.
using System;
using System.Linq;
public class Program
{
public static void Main()
{
var namesList = new string[] { "vishal", "sachin", "sourav", "mahesh", "santosh" };
//Using Method Syntax
var MethodSyntax = namesList.Where(whr=> whr.Contains("v"));
foreach (var msName in MethodSyntax)
{
Console.WriteLine(msName);
}
Console.WriteLine("---------");
//Using Query Syntax
var QuerySyntax = from name in namesList
where name.Contains('v')
select name;
foreach (var qsName in QuerySyntax)
{
Console.WriteLine(qsName);
}
Console.ReadKey();
}
}
Single(): It is used to return the single element from the collection, which satisfies the condition.
Example 1
using System;
using System.Linq;
public class Program
{
public static void Main()
{
int[] IntArray = { 10, 20, 30, 40, 50, 60 };
//Using Method Syntax
//Fetching the Only Element from the Sequenece using Method Syntax
var MethodSyntax = IntArray.Single();
//Using Query Syntax
//Fetching the Only Element from the Sequenece using Query Syntax
var QuerySyntax = (from num in IntArray select num).Single();
Console.WriteLine(MethodSyntax);
Console.WriteLine(QuerySyntax);
}
}
Note. We will get the following exception. This is because now the sequence contains more than one element
Example 2
using System;
using System.Linq;
public class Program
{
public static void Main()
{
int[] IntArray = { 10, 20, 30, 40, 50, 60 };
//Using Method Syntax
//Fetching the Only Element from the Sequenece using Method Syntax
//Where the Element is 40
var MethodSyntax = IntArray.Single(x=> x == 40);
//Using Query Syntax
//Fetching the Only Element from the Sequenece using Query Syntax
//Where the Element is 40
var QuerySyntax = (from num in IntArray select num).Single(num => num == 40);
Console.WriteLine(MethodSyntax);
Console.WriteLine(QuerySyntax);
}
}
Example 3
using System;
using System.Linq;
using System.Collections.Generic;
public class Program
{
public class Employee
{
public int ID { get; set; }
public string Name { get; set; }
public int Salary { get; set; }
public string Department { get; set; }
public static List<Employee> GetAllEmployees()
{
List<Employee> listEmployees = new List<Employee>()
{
new Employee{ID= 101,Name = "Pooja", Salary = 15000, Department = "IT"},
new Employee{ID= 102,Name = "Priyanka", Salary = 15000, Department = "Sales"},
new Employee{ID= 103,Name = "Manoj", Salary = 25000, Department = "Sales"},
new Employee{ID= 104,Name = "Santosh", Salary = 20000, Department = "IT"},
new Employee{ID= 105,Name = "Vishal", Salary = 30000, Department = "IT"},
new Employee{ID= 106,Name = "Sandhya", Salary = 25000, Department = "IT"},
new Employee{ID= 107,Name = "Mahesh", Salary = 35000, Department = "IT"},
new Employee{ID= 108,Name = "Manoj", Salary = 11000, Department = "Sales"},
new Employee{ID= 109,Name = "Pradeep", Salary = 20000, Department = "Sales"},
new Employee{ID= 110,Name = "Saurav", Salary = 25000, Department = "Sales"}
};
return listEmployees;
}
}
public static void Main()
{
//int[] IntArray = { 10, 20, 30, 40, 50, 60 };
//Using Method Syntax
var MethodSyntax = // IntArray.SingleOrDefault(x=> x == 20);
Employee.GetAllEmployees().Single(emp => emp.ID == 102);
//Using Query Syntax
var QuerySyntax = //(from num in IntArray select num).SingleOrDefault(num => num == 10 );
(from emp in Employee.GetAllEmployees() select emp).Single(emp => emp.ID == 106);
Console.WriteLine($"Name: {MethodSyntax.Name} Department: {MethodSyntax.Department} Salary: {MethodSyntax.Salary}");
Console.WriteLine($"Name: {QuerySyntax.Name} Department: {QuerySyntax.Department} Salary: {QuerySyntax.Salary}");
Console.ReadKey();
}
}
SingleOrDefault(): It is used to return a single element from a data source, or you can say from a sequence.
Example 1
using System;
using System.Linq;
public class Program
{
public static void Main()
{
int[] IntArray = { 10, 20, 30, 40, 50, 60 };
//Using Method Syntax
//Fetching the Only Element from the Sequenece using Method Syntax
var MethodSyntax = IntArray.SingleOrDefault(x=> x == 20);
//Using Query Syntax
//Fetching the Only Element from the Sequenece using Query Syntax
var QuerySyntax = (from num in IntArray select num).SingleOrDefault(num => num == 10 );
Console.WriteLine(MethodSyntax);
Console.WriteLine(QuerySyntax);
}
}
Example 2
using System;
using System.Linq;
using System.Collections.Generic;
public class Program
{
public class Employee
{
public int ID { get; set; }
public string Name { get; set; }
public int Salary { get; set; }
public string Department { get; set; }
public static List<Employee> GetAllEmployees()
{
List<Employee> listEmployees = new List<Employee>()
{
new Employee{ID= 101,Name = "Pooja", Salary = 15000, Department = "IT"},
new Employee{ID= 102,Name = "Priyanka", Salary = 15000, Department = "Sales"},
new Employee{ID= 103,Name = "Manoj", Salary = 25000, Department = "Sales"},
new Employee{ID= 104,Name = "Santosh", Salary = 20000, Department = "IT"},
new Employee{ID= 105,Name = "Vishal", Salary = 30000, Department = "IT"},
new Employee{ID= 106,Name = "Sandhya", Salary = 25000, Department = "IT"},
new Employee{ID= 107,Name = "Mahesh", Salary = 35000, Department = "IT"},
new Employee{ID= 108,Name = "Manoj", Salary = 11000, Department = "Sales"},
new Employee{ID= 109,Name = "Pradeep", Salary = 20000, Department = "Sales"},
new Employee{ID= 110,Name = "Saurav", Salary = 25000, Department = "Sales"}
};
return listEmployees;
}
}
public static void Main()
{
//Using Method Syntax
var MethodSyntax = Employee.GetAllEmployees().SingleOrDefault(emp => emp.ID == 105);
//Using Query Syntax
var QuerySyntax = (from emp in Employee.GetAllEmployees() select emp).SingleOrDefault(emp => emp.ID == 109);
Console.WriteLine($"Name: {MethodSyntax.Name} Department: {MethodSyntax.Department} Salary: {MethodSyntax.Salary}");
Console.WriteLine($"Name: {QuerySyntax.Name} Department: {QuerySyntax.Department} Salary: {QuerySyntax.Salary}");
Console.ReadKey();
}
}
First(): It is used to return the first element from a data source or from a collection.
Example 1
using System;
using System.Linq;
public class Program
{
public static void Main()
{
int[] IntArray = { 10, 20, 30, 40, 50, 60 };
//Using Method Syntax
var MethodSyntax = IntArray.First();
//Using Query Syntax
var QuerySyntax = (from num in IntArray select num).First();
Console.WriteLine(MethodSyntax);
Console.WriteLine(QuerySyntax);
}
}
Example 2
using System;
using System.Linq;
using System.Collections.Generic;
public class Program
{
public class Employee
{
public int ID { get; set; }
public string Name { get; set; }
public int Salary { get; set; }
public string Department { get; set; }
public static List<Employee> GetAllEmployees()
{
List<Employee> listEmployees = new List<Employee>()
{
new Employee{ID= 101,Name = "Pooja", Salary = 15000, Department = "IT"},
new Employee{ID= 102,Name = "Priyanka", Salary = 15000, Department = "Sales"},
new Employee{ID= 103,Name = "Manoj", Salary = 25000, Department = "Sales"},
new Employee{ID= 104,Name = "Santosh", Salary = 20000, Department = "IT"},
new Employee{ID= 105,Name = "Vishal", Salary = 30000, Department = "IT"},
new Employee{ID= 106,Name = "Sandhya", Salary = 25000, Department = "IT"},
new Employee{ID= 107,Name = "Mahesh", Salary = 35000, Department = "IT"},
new Employee{ID= 108,Name = "Manoj", Salary = 11000, Department = "Sales"},
new Employee{ID= 109,Name = "Pradeep", Salary = 20000, Department = "Sales"},
new Employee{ID= 110,Name = "Saurav", Salary = 25000, Department = "Sales"}
};
return listEmployees;
}
}
public static void Main()
{
//Using Method Syntax
var MethodSyntax = Employee.GetAllEmployees().First();
//Using Query Syntax
var QuerySyntax = (from emp in Employee.GetAllEmployees() select emp).First();
Console.WriteLine($"Name: {MethodSyntax.Name} Department: {MethodSyntax.Department} Salary: {MethodSyntax.Salary}");
Console.WriteLine($"Name: {QuerySyntax.Name} Department: {QuerySyntax.Department} Salary: {QuerySyntax.Salary}");
Console.ReadKey();
}
}
Example 3
using System;
using System.Linq;
using System.Collections.Generic;
public class Program
{
public class Employee
{
public int ID { get; set; }
public string Name { get; set; }
public int Salary { get; set; }
public string Department { get; set; }
public static List<Employee> GetAllEmployees()
{
List<Employee> listEmployees = new List<Employee>()
{
new Employee{ID= 101,Name = "Pooja", Salary = 15000, Department = "IT"},
new Employee{ID= 102,Name = "Priyanka", Salary = 15000, Department = "Sales"},
new Employee{ID= 103,Name = "Manoj", Salary = 25000, Department = "Sales"},
new Employee{ID= 104,Name = "Santosh", Salary = 20000, Department = "IT"},
new Employee{ID= 105,Name = "Vishal", Salary = 30000, Department = "IT"},
new Employee{ID= 106,Name = "Sandhya", Salary = 25000, Department = "IT"},
new Employee{ID= 107,Name = "Mahesh", Salary = 35000, Department = "IT"},
new Employee{ID= 108,Name = "Manoj", Salary = 11000, Department = "Sales"},
new Employee{ID= 109,Name = "Pradeep", Salary = 20000, Department = "Sales"},
new Employee{ID= 110,Name = "Saurav", Salary = 25000, Department = "Sales"}
};
return listEmployees;
}
}
public static void Main()
{
//Using Method Syntax
var MethodSyntax = Employee.GetAllEmployees().First(x => x.ID == 105);
//Using Query Syntax
var QuerySyntax = (from emp in Employee.GetAllEmployees() select emp).First(x => x.ID == 108);
Console.WriteLine($"Name: {MethodSyntax.Name} Department: {MethodSyntax.Department} Salary: {MethodSyntax.Salary}");
Console.WriteLine($"Name: {QuerySyntax.Name} Department: {QuerySyntax.Department} Salary: {QuerySyntax.Salary}");
Console.ReadKey();
}
}
FirstOrDefault(): It is used to return the first element of a sequence or a default value if the element isn't there.
Example 1
using System;
using System.Linq;
public class Program
{
public static void Main()
{
int[] IntArray = { 10, 20, 30, 40, 50, 60 };
//Using Method Syntax
var MethodSyntax = IntArray.FirstOrDefault();
//Using Query Syntax
var QuerySyntax = (from num in IntArray select num).FirstOrDefault();
Console.WriteLine(MethodSyntax);
Console.WriteLine(QuerySyntax);
}
}
Example 2
using System;
using System.Linq;
using System.Collections.Generic;
public class Program
{
public class Employee
{
public int ID { get; set; }
public string Name { get; set; }
public int Salary { get; set; }
public string Department { get; set; }
public static List<Employee> GetAllEmployees()
{
List<Employee> listEmployees = new List<Employee>()
{
new Employee{ID= 101,Name = "Pooja", Salary = 15000, Department = "IT"},
new Employee{ID= 102,Name = "Priyanka", Salary = 15000, Department = "Sales"},
new Employee{ID= 103,Name = "Manoj", Salary = 25000, Department = "Sales"},
new Employee{ID= 104,Name = "Santosh", Salary = 20000, Department = "IT"},
new Employee{ID= 105,Name = "Vishal", Salary = 30000, Department = "IT"},
new Employee{ID= 106,Name = "Sandhya", Salary = 25000, Department = "IT"},
new Employee{ID= 107,Name = "Mahesh", Salary = 35000, Department = "IT"},
new Employee{ID= 108,Name = "Manoj", Salary = 11000, Department = "Sales"},
new Employee{ID= 109,Name = "Pradeep", Salary = 20000, Department = "Sales"},
new Employee{ID= 110,Name = "Saurav", Salary = 25000, Department = "Sales"}
};
return listEmployees;
}
}
public static void Main()
{
//Using Method Syntax
var MethodSyntax = Employee.GetAllEmployees().FirstOrDefault();
//Using Query Syntax
var QuerySyntax = (from emp in Employee.GetAllEmployees() select emp).FirstOrDefault();
Console.WriteLine($"Name: {MethodSyntax.Name} Department: {MethodSyntax.Department} Salary: {MethodSyntax.Salary}");
Console.WriteLine($"Name: {QuerySyntax.Name} Department: {QuerySyntax.Department} Salary: {QuerySyntax.Salary}");
Console.ReadKey();
}
}
Example 3
using System;
using System.Linq;
using System.Collections.Generic;
public class Program
{
public class Employee
{
public int ID { get; set; }
public string Name { get; set; }
public int Salary { get; set; }
public string Department { get; set; }
public static List<Employee> GetAllEmployees()
{
List<Employee> listEmployees = new List<Employee>()
{
new Employee{ID= 101,Name = "Pooja", Salary = 15000, Department = "IT"},
new Employee{ID= 102,Name = "Priyanka", Salary = 15000, Department = "Sales"},
new Employee{ID= 103,Name = "Manoj", Salary = 25000, Department = "Sales"},
new Employee{ID= 104,Name = "Santosh", Salary = 20000, Department = "IT"},
new Employee{ID= 105,Name = "Vishal", Salary = 30000, Department = "IT"},
new Employee{ID= 106,Name = "Sandhya", Salary = 25000, Department = "IT"},
new Employee{ID= 107,Name = "Mahesh", Salary = 35000, Department = "IT"},
new Employee{ID= 108,Name = "Manoj", Salary = 11000, Department = "Sales"},
new Employee{ID= 109,Name = "Pradeep", Salary = 20000, Department = "Sales"},
new Employee{ID= 110,Name = "Saurav", Salary = 25000, Department = "Sales"}
};
return listEmployees;
}
}
public static void Main()
{
//Using Method Syntax
var MethodSyntax = Employee.GetAllEmployees().FirstOrDefault(x => x.Department == "IT");
//Using Query Syntax
var QuerySyntax = (from emp in Employee.GetAllEmployees() select emp).FirstOrDefault(x => x.Department == "Sales");
Console.WriteLine($"Name: {MethodSyntax.Name} Department: {MethodSyntax.Department} Salary: {MethodSyntax.Salary}");
Console.WriteLine($"Name: {QuerySyntax.Name} Department: {QuerySyntax.Department} Salary: {QuerySyntax.Salary}");
Console.ReadKey();
}
}
Last(): It is used to return the last element from a data source or from a collection.
Example 1
using System;
using System.Linq;
public class Program
{
public static void Main()
{
int[] IntArray = { 10, 20, 30, 40, 50, 60 };
//Using Method Syntax
var MethodSyntax = IntArray.Last();
//Using Query Syntax
var QuerySyntax = (from num in IntArray select num).Last();
Console.WriteLine(MethodSyntax);
Console.WriteLine(QuerySyntax);
}
}
Example 2
using System;
using System.Linq;
using System.Collections.Generic;
public class Program
{
public class Employee
{
public int ID { get; set; }
public string Name { get; set; }
public int Salary { get; set; }
public string Department { get; set; }
public static List<Employee> GetAllEmployees()
{
List<Employee> listEmployees = new List<Employee>()
{
new Employee{ID= 101,Name = "Pooja", Salary = 15000, Department = "IT"},
new Employee{ID= 102,Name = "Priyanka", Salary = 15000, Department = "Sales"},
new Employee{ID= 103,Name = "Manoj", Salary = 25000, Department = "Sales"},
new Employee{ID= 104,Name = "Santosh", Salary = 20000, Department = "IT"},
new Employee{ID= 105,Name = "Vishal", Salary = 30000, Department = "IT"},
new Employee{ID= 106,Name = "Sandhya", Salary = 25000, Department = "IT"},
new Employee{ID= 107,Name = "Mahesh", Salary = 35000, Department = "IT"},
new Employee{ID= 108,Name = "Manoj", Salary = 11000, Department = "Sales"},
new Employee{ID= 109,Name = "Pradeep", Salary = 20000, Department = "Sales"},
new Employee{ID= 110,Name = "Saurav", Salary = 25000, Department = "Sales"}
};
return listEmployees;
}
}
public static void Main()
{
//Using Method Syntax
var MethodSyntax = Employee.GetAllEmployees().Last();
//Using Query Syntax
var QuerySyntax = (from emp in Employee.GetAllEmployees() select emp).Last();
Console.WriteLine($"Name: {MethodSyntax.Name} Department: {MethodSyntax.Department} Salary: {MethodSyntax.Salary}");
Console.WriteLine($"Name: {QuerySyntax.Name} Department: {QuerySyntax.Department} Salary: {QuerySyntax.Salary}");
Console.ReadKey();
}
}
Example 3
using System;
using System.Linq;
using System.Collections.Generic;
public class Program
{
public class Employee
{
public int ID { get; set; }
public string Name { get; set; }
public int Salary { get; set; }
public string Department { get; set; }
public static List<Employee> GetAllEmployees()
{
List<Employee> listEmployees = new List<Employee>()
{
new Employee{ID= 101,Name = "Pooja", Salary = 15000, Department = "IT"},
new Employee{ID= 102,Name = "Priyanka", Salary = 15000, Department = "Sales"},
new Employee{ID= 103,Name = "Manoj", Salary = 25000, Department = "Sales"},
new Employee{ID= 104,Name = "Santosh", Salary = 20000, Department = "IT"},
new Employee{ID= 105,Name = "Vishal", Salary = 30000, Department = "IT"},
new Employee{ID= 106,Name = "Sandhya", Salary = 25000, Department = "IT"},
new Employee{ID= 107,Name = "Mahesh", Salary = 35000, Department = "IT"},
new Employee{ID= 108,Name = "Manoj", Salary = 11000, Department = "Sales"},
new Employee{ID= 109,Name = "Pradeep", Salary = 20000, Department = "Sales"},
new Employee{ID= 110,Name = "Saurav", Salary = 25000, Department = "Sales"}
};
return listEmployees;
}
}
public static void Main()
{
//Using Method Syntax
var MethodSyntax = Employee.GetAllEmployees().Last(x => x.Department == "IT");
//IntArray.Last();
//Using Query Syntax
var QuerySyntax = (from emp in Employee.GetAllEmployees() select emp).Last(x => x.Department == "Sales");
Console.WriteLine($"Name: {MethodSyntax.Name} Department: {MethodSyntax.Department} Salary: {MethodSyntax.Salary}");
Console.WriteLine($"Name: {QuerySyntax.Name} Department: {QuerySyntax.Department} Salary: {QuerySyntax.Salary}");
Console.ReadKey();
}
}
LastOrDefault(): It is used to return the last element of a sequence or a default value if the element isn't there.
Example 1
using System;
using System.Linq;
public class Program
{
public static void Main()
{
int[] IntArray = { 10, 20, 30, 40, 50, 60 };
//Using Method Syntax
var MethodSyntax = IntArray.LastOrDefault();
//Using Query Syntax
var QuerySyntax = (from num in IntArray select num).LastOrDefault();
Console.WriteLine(MethodSyntax);
Console.WriteLine(QuerySyntax);
}
}
Example 2
using System;
using System.Linq;
using System.Collections.Generic;
public class Program
{
public class Employee
{
public int ID { get; set; }
public string Name { get; set; }
public int Salary { get; set; }
public string Department { get; set; }
public static List<Employee> GetAllEmployees()
{
List<Employee> listEmployees = new List<Employee>()
{
new Employee{ID= 101,Name = "Pooja", Salary = 15000, Department = "IT"},
new Employee{ID= 102,Name = "Priyanka", Salary = 15000, Department = "Sales"},
new Employee{ID= 103,Name = "Manoj", Salary = 25000, Department = "Sales"},
new Employee{ID= 104,Name = "Santosh", Salary = 20000, Department = "IT"},
new Employee{ID= 105,Name = "Vishal", Salary = 30000, Department = "IT"},
new Employee{ID= 106,Name = "Sandhya", Salary = 25000, Department = "IT"},
new Employee{ID= 107,Name = "Mahesh", Salary = 35000, Department = "IT"},
new Employee{ID= 108,Name = "Manoj", Salary = 11000, Department = "Sales"},
new Employee{ID= 109,Name = "Pradeep", Salary = 20000, Department = "Sales"},
new Employee{ID= 110,Name = "Saurav", Salary = 25000, Department = "Sales"}
};
return listEmployees;
}
}
public static void Main()
{
//Using Method Syntax
var MethodSyntax = Employee.GetAllEmployees().LastOrDefault();
//Using Query Syntax
var QuerySyntax = (from emp in Employee.GetAllEmployees() select emp).LastOrDefault();
Console.WriteLine($"Name: {MethodSyntax.Name} Department: {MethodSyntax.Department} Salary: {MethodSyntax.Salary}");
Console.WriteLine($"Name: {QuerySyntax.Name} Department: {QuerySyntax.Department} Salary: {QuerySyntax.Salary}");
Console.ReadKey();
}
}
All(): It is used to check whether all the elements of a data source satisfy a given condition or not.
Example 1
using System;
using System.Collections.Generic;
using System.Linq;
public class Program
{
public class Employee
{
public int ID { get; set; }
public string Name { get; set; }
public int Salary { get; set; }
public string Department { get; set; }
public static List<Employee> GetAllEmployees()
{
List<Employee> listEmployees = new List<Employee>()
{
new Employee{ID= 101,Name = "Pooja", Salary = 15000, Department = "IT"},
new Employee{ID= 102,Name = "Priyanka", Salary = 15000, Department = "Sales"},
new Employee{ID= 103,Name = "Manoj", Salary = 25000, Department = "Sales"},
new Employee{ID= 104,Name = "Santosh", Salary = 20000, Department = "IT"},
new Employee{ID= 105,Name = "Vishal", Salary = 30000, Department = "IT"},
new Employee{ID= 106,Name = "Sandhya", Salary = 25000, Department = "IT"},
new Employee{ID= 107,Name = "Mahesh", Salary = 35000, Department = "IT"},
new Employee{ID= 108,Name = "Manoj", Salary = 11000, Department = "Sales"},
new Employee{ID= 109,Name = "Pradeep", Salary = 20000, Department = "Sales"},
new Employee{ID= 110,Name = "Saurav", Salary = 25000, Department = "Sales"}
};
return listEmployees;
}
}
public static void Main()
{
var result = Employee.GetAllEmployees().All(whr => whr.Salary >= 10000);
if(result)
{
Console.WriteLine($"All employees have salary greater than 10000");
}
Console.ReadKey();
}
}
Example 2
using System;
using System.Linq;
public class Program
{
public static void Main()
{
int[] numbers = { 2, 4, 6, 8 };
bool allEvenNumbers = numbers.All(n => n % 2 == 0 ? true : false);
if (allEvenNumbers)
{
Console.WriteLine($"All numbers are even.");
}
Console.ReadKey();
}
}
ToDictionary(): Creates a dictionary from a sequence of key-value pairs.
Example
using System;
using System.Collections.Generic;
using System.Linq;
public class Program
{
public class Employee
{
public int ID { get; set; }
public string Name { get; set; }
public int Salary { get; set; }
public string Department { get; set; }
public static List<Employee> GetAllEmployees()
{
List<Employee> listEmployees = new List<Employee>()
{
new Employee{ID= 101,Name = "Pooja", Salary = 15000, Department = "IT"},
new Employee{ID= 102,Name = "Priyanka", Salary = 15000, Department = "Sales"},
new Employee{ID= 103,Name = "Manoj", Salary = 25000, Department = "Sales"},
new Employee{ID= 104,Name = "Santosh", Salary = 20000, Department = "IT"},
new Employee{ID= 105,Name = "Vishal", Salary = 30000, Department = "IT"},
new Employee{ID= 106,Name = "Sandhya", Salary = 25000, Department = "IT"},
new Employee{ID= 107,Name = "Mahesh", Salary = 35000, Department = "IT"},
new Employee{ID= 108,Name = "Manoj", Salary = 11000, Department = "Sales"},
new Employee{ID= 109,Name = "Pradeep", Salary = 20000, Department = "Sales"},
new Employee{ID= 110,Name = "Saurav", Salary = 25000, Department = "Sales"}
};
return listEmployees;
}
}
public static void Main()
{
Dictionary<int, string> employeeDictionarys = Employee.GetAllEmployees().ToDictionary(x => x.ID, x => x.Name);
foreach (KeyValuePair<int, string> kvp in employeeDictionarys)
{
Console.WriteLine($"Key : {kvp.Key} Value : {kvp.Value}");
}
Console.ReadKey();
}
}
Any(): It is used to check whether at least one of the elements of a data source satisfies a given condition or not.
Example 1
using System;
using System.Collections.Generic;
using System.Linq;
public class Program
{
public class Employee
{
public int ID { get; set; }
public string Name { get; set; }
public int Salary { get; set; }
public string Department { get; set; }
public static List<Employee> GetAllEmployees()
{
List<Employee> listEmployees = new List<Employee>()
{
new Employee{ID= 101,Name = "Pooja", Salary = 15000, Department = "IT"},
new Employee{ID= 102,Name = "Priyanka", Salary = 15000, Department = "Sales"},
new Employee{ID= 103,Name = "Manoj", Salary = 25000, Department = "Sales"},
new Employee{ID= 104,Name = "Santosh", Salary = 20000, Department = "IT"},
new Employee{ID= 105,Name = "Vishal", Salary = 30000, Department = "IT"},
new Employee{ID= 106,Name = "Sandhya", Salary = 25000, Department = "IT"},
new Employee{ID= 107,Name = "Mahesh", Salary = 35000, Department = "IT"},
new Employee{ID= 108,Name = "Manoj", Salary = 11000, Department = "Sales"},
new Employee{ID= 109,Name = "Pradeep", Salary = 20000, Department = "Sales"},
new Employee{ID= 110,Name = "Saurav", Salary = 25000, Department = "Sales"}
};
return listEmployees;
}
}
public static void Main()
{
var result = Employee.GetAllEmployees().Any(x=> x.Salary > 20000 && x.Department == "IT");
if (result)
{
Console.WriteLine($"At least one employee in IT Department with salary higher than 20000");
}
Console.ReadKey();
}
}
OrderBy(): It is used to sort the data in Ascending Order.
Example 1
using System;
using System.Linq;
public class Program
{
public static void Main()
{
int[] IntArray = { 10, 30, 50, 40, 20, 60 };
//Using Method Syntax
var MethodSyntax = IntArray.OrderBy(num => num);
//Using Query Syntax
var QuerySyntax = (from num in IntArray orderby num select num).ToList();
Console.WriteLine("After Sorting Method Syntax");
foreach (var item in MethodSyntax)
{
Console.Write($"{item} ");
}
Console.WriteLine("After Sorting Query Syntax");
foreach (var item in QuerySyntax)
{
Console.Write($"{item} ");
}
}
}
Example 2
using System;
using System.Linq;
using System.Collections.Generic;
public class Program
{
public class Employee
{
public int ID { get; set; }
public string Name { get; set; }
public int Salary { get; set; }
public string Department { get; set; }
public static List<Employee> GetAllEmployees()
{
List<Employee> listEmployees = new List<Employee>()
{
new Employee{ID= 101,Name = "Pooja", Salary = 15000, Department = "IT"},
new Employee{ID= 102,Name = "Priyanka", Salary = 15000, Department = "Sales"},
new Employee{ID= 103,Name = "Manoj", Salary = 25000, Department = "Sales"},
new Employee{ID= 104,Name = "Santosh", Salary = 20000, Department = "IT"},
new Employee{ID= 105,Name = "Vishal", Salary = 30000, Department = "IT"},
new Employee{ID= 106,Name = "Sandhya", Salary = 25000, Department = "IT"},
new Employee{ID= 107,Name = "Mahesh", Salary = 35000, Department = "IT"},
new Employee{ID= 108,Name = "Manoj", Salary = 11000, Department = "Sales"},
new Employee{ID= 109,Name = "Pradeep", Salary = 20000, Department = "Sales"},
new Employee{ID= 110,Name = "Saurav", Salary = 25000, Department = "Sales"}
};
return listEmployees;
}
}
public static void Main()
{
//Using Method Syntax
var MethodSyntax = Employee.GetAllEmployees().OrderBy(ord => ord.Name).ToList();
//Using Query Syntax
var QuerySyntax = (from emp in Employee.GetAllEmployees() orderby emp.Name select emp).ToList();
Console.WriteLine("After Sorting Method Syntax: ");
foreach (var item in MethodSyntax)
{
Console.Write($"Name: {item.Name} ");
}
Console.WriteLine("After Sorting Query Syntax: ");
foreach (var item in QuerySyntax)
{
Console.Write($"Name: {item.Name} ");
}
Console.ReadKey();
}
}
OrderByDescending(): It is used to sort the data in Descending order.
Example 1
using System;
using System.Collections.Generic;
using System.Linq;
public class Program
{
public static void Main()
{
List<int> intList = new List<int>() { 10, 45, 35, 29, 100, 69, 58, 50 };
//Sorting the data in Descending Order Using Method Syntax
var MethodSyntax = intList.OrderByDescending(num => num);
//Sorting the data in Descending Order Using Query Syntax
var QuerySyntax = (from num in intList orderby num descending select num).ToList();
Console.WriteLine();
Console.WriteLine("After Sorting the Data in Descending Order by Method Syntax: ");
foreach (var item in MethodSyntax)
{
Console.WriteLine($"{item} ");
}
Console.WriteLine();
Console.WriteLine("After Sorting the Data in Descending Order by Query Syntax: ");
foreach (var item in QuerySyntax)
{
Console.WriteLine($"{item} ");
}
Console.ReadKey();
}
}
Example 2
using System;
using System.Collections.Generic;
using System.Linq;
public class Program
{
public class Employee
{
public int ID { get; set; }
public string Name { get; set; }
public int Salary { get; set; }
public string Department { get; set; }
public static List<Employee> GetAllEmployees()
{
List<Employee> listEmployees = new List<Employee>()
{
new Employee{ID= 101,Name = "Pooja", Salary = 15000, Department = "IT"},
new Employee{ID= 102,Name = "Priyanka", Salary = 15000, Department = "Sales"},
new Employee{ID= 103,Name = "Manoj", Salary = 25000, Department = "Sales"},
new Employee{ID= 104,Name = "Santosh", Salary = 20000, Department = "IT"},
new Employee{ID= 105,Name = "Vishal", Salary = 30000, Department = "IT"},
new Employee{ID= 106,Name = "Sandhya", Salary = 25000, Department = "IT"},
new Employee{ID= 107,Name = "Mahesh", Salary = 35000, Department = "IT"},
new Employee{ID= 108,Name = "Manoj", Salary = 11000, Department = "Sales"},
new Employee{ID= 109,Name = "Pradeep", Salary = 20000, Department = "Sales"},
new Employee{ID= 110,Name = "Saurav", Salary = 25000, Department = "Sales"}
};
return listEmployees;
}
}
public static void Main()
{
//Sorting in Descending Order Using Method Syntax
var MethodSyntax = Employee.GetAllEmployees().OrderByDescending(ord => ord.Name);
//Sorting in Descending Order Using Query Syntax
var QuerySyntax = (from emp in Employee.GetAllEmployees()
orderby emp.Name descending
select emp).ToList();
Console.WriteLine();
Console.WriteLine("After Reverse Data by Method Syntax: ");
foreach (var item in MethodSyntax)
{
Console.WriteLine($"{item.Name} ");
}
Console.WriteLine();
Console.WriteLine("After Reverse Data by Query Syntax: ");
foreach (var item in QuerySyntax)
{
Console.WriteLine($"{item.Name} ");
}
Console.ReadKey();
}
}
ThenBy(): It is used to sort the data in Ascending order from the second level onwards.
Example
using System;
using System.Linq;
using System.Collections.Generic;
public class Program
{
public class Employee
{
public int ID { get; set; }
public string Name { get; set; }
public int Salary { get; set; }
public string Department { get; set; }
public static List<Employee> GetAllEmployees()
{
List<Employee> listEmployees = new List<Employee>()
{
new Employee{ID= 101,Name = "Pooja", Salary = 15000, Department = "IT"},
new Employee{ID= 102,Name = "Priyanka", Salary = 15000, Department = "Sales"},
new Employee{ID= 103,Name = "Manoj", Salary = 25000, Department = "Sales"},
new Employee{ID= 104,Name = "Santosh", Salary = 20000, Department = "IT"},
new Employee{ID= 105,Name = "Vishal", Salary = 30000, Department = "IT"},
new Employee{ID= 106,Name = "Sandhya", Salary = 25000, Department = "IT"},
new Employee{ID= 107,Name = "Mahesh", Salary = 35000, Department = "IT"},
new Employee{ID= 108,Name = "Manoj", Salary = 11000, Department = "Sales"},
new Employee{ID= 109,Name = "Pradeep", Salary = 20000, Department = "Sales"},
new Employee{ID= 110,Name = "Saurav", Salary = 25000, Department = "Sales"}
};
return listEmployees;
}
}
public static void Main()
{
//Using Method Syntax
var MethodSyntax = Employee.GetAllEmployees()
.OrderBy(ord => ord.Department)
.ThenBy(x => x.Name)
.ToList();
//Using Query Syntax
var QuerySyntax = (from emp in Employee.GetAllEmployees()
orderby emp.Department, emp.Name
select emp);
Console.WriteLine("After Sorting Method Syntax: ");
foreach (var item in MethodSyntax)
{
Console.Write($"Department: {item.Department} Name: {item.Name} ");
}
Console.WriteLine("\n After Sorting Query Syntax: ");
foreach (var item in QuerySyntax)
{
Console.Write($"Department: {item.Department} Name: {item.Name} ");
}
Console.ReadKey();
}
}
ThenByDescending() - It is used to sort the data in Descending order also from the second level onwards.
Example
using System;
using System.Collections.Generic;
using System.Linq;
public class Program
{
public class Employee
{
public int ID { get; set; }
public string Name { get; set; }
public int Salary { get; set; }
public string Department { get; set; }
public static List<Employee> GetAllEmployees()
{
List<Employee> listEmployees = new List<Employee>()
{
new Employee{ID= 101,Name = "Pooja", Salary = 15000, Department = "IT"},
new Employee{ID= 102,Name = "Priyanka", Salary = 15000, Department = "Sales"},
new Employee{ID= 103,Name = "Manoj", Salary = 25000, Department = "Sales"},
new Employee{ID= 104,Name = "Santosh", Salary = 20000, Department = "IT"},
new Employee{ID= 105,Name = "Vishal", Salary = 30000, Department = "IT"},
new Employee{ID= 106,Name = "Sandhya", Salary = 25000, Department = "IT"},
new Employee{ID= 107,Name = "Mahesh", Salary = 35000, Department = "IT"},
new Employee{ID= 108,Name = "Manoj", Salary = 11000, Department = "Sales"},
new Employee{ID= 109,Name = "Pradeep", Salary = 20000, Department = "Sales"},
new Employee{ID= 110,Name = "Saurav", Salary = 25000, Department = "Sales"}
};
return listEmployees;
}
}
public static void Main()
{
//Using Method Syntax
var MethodSyntax = Employee.GetAllEmployees()
.Where(whr=> whr.Department == "IT")
.OrderByDescending(ord=> ord.Salary)
.ThenByDescending(x=> x.Name);
//Using Query Syntax
var QuerySyntax = (from emp in Employee.GetAllEmployees()
where emp.Department == "Sales"
orderby emp.Salary descending,
emp.Name descending
select emp).ToList();
Console.WriteLine();
Console.WriteLine("Method Syntax: ");
foreach (var item in MethodSyntax)
{
Console.WriteLine($"{item.Department} - {item.Name} - {item.Salary}");
}
Console.WriteLine();
Console.WriteLine("Query Syntax: ");
foreach (var item in QuerySyntax)
{
Console.WriteLine($"{item.Department} - {item.Name} - {item.Salary}");
}
Console.ReadKey();
}
}
Reverse(): It is used to reverse the data stored in a data source.
Example 1
using System;
using System.Collections.Generic;
using System.Linq;
public class Program
{
public static void Main()
{
int[] intList = new int[] { 10, 45, 35, 29, 100, 69, 58, 50 };
//Using Method Syntax
var MethodSyntax = intList.Reverse();
//Using Query Syntax
var QuerySyntax = (from num in intList select num).Reverse();
Console.WriteLine();
Console.WriteLine("After Reverse Data by Method Syntax: ");
foreach (var item in MethodSyntax)
{
Console.WriteLine($"{item}");
}
Console.WriteLine();
Console.WriteLine("After Reverse Data by Query Syntax: ");
foreach (var item in QuerySyntax)
{
Console.WriteLine($"{item}");
}
Console.ReadKey();
}
}
Example 2
using System;
using System.Collections.Generic;
using System.Linq;
public class Program
{
public class Employee
{
public int ID { get; set; }
public string Name { get; set; }
public int Salary { get; set; }
public string Department { get; set; }
public static List<Employee> GetAllEmployees()
{
List<Employee> listEmployees = new List<Employee>()
{
new Employee{ID= 101,Name = "Pooja", Salary = 15000, Department = "IT"},
new Employee{ID= 102,Name = "Priyanka", Salary = 15000, Department = "Sales"},
new Employee{ID= 103,Name = "Manoj", Salary = 25000, Department = "Sales"},
new Employee{ID= 104,Name = "Santosh", Salary = 20000, Department = "IT"},
new Employee{ID= 105,Name = "Vishal", Salary = 30000, Department = "IT"},
new Employee{ID= 106,Name = "Sandhya", Salary = 25000, Department = "IT"},
new Employee{ID= 107,Name = "Mahesh", Salary = 35000, Department = "IT"},
new Employee{ID= 108,Name = "Manoj", Salary = 11000, Department = "Sales"},
new Employee{ID= 109,Name = "Pradeep", Salary = 20000, Department = "Sales"},
new Employee{ID= 110,Name = "Saurav", Salary = 25000, Department = "Sales"}
};
return listEmployees;
}
}
public static void Main()
{
//Using Method Syntax
var MethodSyntax = Employee.GetAllEmployees().Select(sel=> sel.Name).Reverse();
//Using Query Syntax
var QuerySyntax = (from emp in Employee.GetAllEmployees()
select emp.Name).Reverse();
Console.WriteLine();
Console.WriteLine("After Reverse Data by Method Syntax: ");
foreach (var item in MethodSyntax)
{
Console.WriteLine($"{item} ");
}
Console.WriteLine();
Console.WriteLine("After Reverse Data by Method Syntax: ");
foreach (var item in QuerySyntax)
{
Console.WriteLine($"{item} ");
}
Console.ReadKey();
}
}
Average(): It is used to calculate the average of numeric values from the collection on which it is applied.
Example 1
using System;
using System.Linq;
public class Program
{
public static void Main()
{
int[] intNumbers = new int[] { 10, 20, 30, 40, 50, 60, 70, 80, 90, 100 };
//Using Method Syntax
var MethodSyntaxAvgValue = intNumbers.Average();
//Using Query Syntax
var QuerySyntaxAvgValue = (from num in intNumbers select num).Average();
Console.WriteLine($"Method Syntax Average Value = {MethodSyntaxAvgValue}");
Console.WriteLine($"Query Syntax Average Value = {QuerySyntaxAvgValue}");
Console.ReadKey();
}
}
Example 2: Average with where condition (Where extension method).
using System;
using System.Linq;
public class Program
{
public static void Main()
{
int[] intNumbers = new int[] { 10, 20, 30, 40, 50, 60, 70, 80, 90, 100 };
//Using Method Syntax
var MethodSyntaxAvgValue = intNumbers.Where(whr=> whr > 50).Average();
//Using Query Syntax
var QuerySyntaxAvgValue = (from num in intNumbers
where num > 50
select num).Average();
Console.WriteLine($"Method Syntax Average Value = {MethodSyntaxAvgValue}");
Console.WriteLine($"Query Syntax Average Value = {QuerySyntaxAvgValue}");
Console.ReadKey();
}
}
Example 3: Complex Type with Employee Data
using System;
using System.Collections.Generic;
using System.Linq;
public class Program
{
public class Employee
{
public int ID { get; set; }
public string Name { get; set; }
public int Salary { get; set; }
public string Department { get; set; }
public static List<Employee> GetAllEmployees()
{
List<Employee> listEmployees = new List<Employee>()
{
new Employee{ID= 101,Name = "Pooja", Salary = 10000, Department = "IT"},
new Employee{ID= 102,Name = "Priyanka", Salary = 15000, Department = "Sales"},
new Employee{ID= 103,Name = "Manoj", Salary = 25000, Department = "Sales"},
new Employee{ID= 104,Name = "Santosh", Salary = 20000, Department = "IT"},
new Employee{ID= 105,Name = "Vishal", Salary = 30000, Department = "IT"},
new Employee{ID= 106,Name = "Sandhya", Salary = 25000, Department = "IT"},
new Employee{ID= 107,Name = "Mahesh", Salary = 35000, Department = "IT"},
new Employee{ID= 108,Name = "Manoj", Salary = 11000, Department = "Sales"},
new Employee{ID= 109,Name = "Pradeep", Salary = 20000, Department = "Sales"},
new Employee{ID= 110,Name = "Saurav", Salary = 25000, Department = "Sales"}
};
return listEmployees;
}
}
public static void Main()
{
//Using Method Syntax
var MethodSyntaxAvgValue = Employee.GetAllEmployees().Average(emp => emp.Salary);
//Using Query Syntax
var QuerySyntaxAvgValue = (from emp in Employee.GetAllEmployees()
select emp).Average(e => e.Salary);
Console.WriteLine($"Method Syntax Average Value = {MethodSyntaxAvgValue}");
Console.WriteLine($"Query Syntax Average Value = {QuerySyntaxAvgValue}");
Console.ReadKey();
}
}
Example 4: Complex Type with Employe Data with Where a condition
using System;
using System.Collections.Generic;
using System.Linq;
public class Program
{
public class Employee
{
public int ID { get; set; }
public string Name { get; set; }
public int Salary { get; set; }
public string Department { get; set; }
public static List<Employee> GetAllEmployees()
{
List<Employee> listEmployees = new List<Employee>()
{
new Employee{ID= 101,Name = "Pooja", Salary = 10000, Department = "IT"},
new Employee{ID= 102,Name = "Priyanka", Salary = 15000, Department = "Sales"},
new Employee{ID= 103,Name = "Manoj", Salary = 25000, Department = "Sales"},
new Employee{ID= 104,Name = "Santosh", Salary = 20000, Department = "IT"},
new Employee{ID= 105,Name = "Vishal", Salary = 30000, Department = "IT"},
new Employee{ID= 106,Name = "Sandhya", Salary = 25000, Department = "IT"},
new Employee{ID= 107,Name = "Mahesh", Salary = 35000, Department = "IT"},
new Employee{ID= 108,Name = "Manoj", Salary = 11000, Department = "Sales"},
new Employee{ID= 109,Name = "Pradeep", Salary = 20000, Department = "Sales"},
new Employee{ID= 110,Name = "Saurav", Salary = 25000, Department = "Sales"}
};
return listEmployees;
}
}
public static void Main()
{
//Using Method Syntax
var MethodSyntaxAvgValue = Employee.GetAllEmployees()
.Where(whr=> whr.Department == "IT")
.Average(emp => emp.Salary);
//Using Query Syntax
var QuerySyntaxAvgValue = (from emp in Employee.GetAllEmployees()
where emp.Department == "Sales"
select emp).Average(e => e.Salary);
Console.WriteLine($"Method Syntax Average Value for IT Department = {MethodSyntaxAvgValue}");
Console.WriteLine($"Query Syntax Average Value for Sales Department= {QuerySyntaxAvgValue}");
Console.ReadKey();
}
}
Min(), Max(): It returns the largest and smallest number in the list, respectively.
Example 1
using System;
using System.Collections.Generic;
using System.Linq;
public class Program
{
public static void Main()
{
int[] intList = new int[] { 10, 45, 35, 29, 100, 69, 58, 50 };
//Using Method Syntax
var MethodSyntaxMax = intList.Max();
var MethodSyntaxMin = intList.Min();
//Using Query Syntax
var QuerySyntaxMax = (from num in intList select num).Max();
var QuerySyntaxMin = (from num in intList select num).Min();
Console.WriteLine($"Largest number by Method Syntax:{MethodSyntaxMax}");
Console.WriteLine($"Largest number by Method Syntax:{MethodSyntaxMin}");
Console.WriteLine($"Largest number by Query Syntax:{QuerySyntaxMax}");
Console.WriteLine($"Largest number by Query Syntax:{QuerySyntaxMin}");
Console.ReadKey();
}
}
Example 2
using System;
using System.Collections.Generic;
using System.Linq;
public class Program
{
public class Employee
{
public int ID { get; set; }
public string Name { get; set; }
public int Salary { get; set; }
public string Department { get; set; }
public static List<Employee> GetAllEmployees()
{
List<Employee> listEmployees = new List<Employee>()
{
new Employee{ID= 101,Name = "Pooja", Salary = 15000, Department = "IT"},
new Employee{ID= 102,Name = "Priyanka", Salary = 15000, Department = "Sales"},
new Employee{ID= 103,Name = "Manoj", Salary = 25000, Department = "Sales"},
new Employee{ID= 104,Name = "Santosh", Salary = 20000, Department = "IT"},
new Employee{ID= 105,Name = "Vishal", Salary = 30000, Department = "IT"},
new Employee{ID= 106,Name = "Sandhya", Salary = 25000, Department = "IT"},
new Employee{ID= 107,Name = "Mahesh", Salary = 35000, Department = "IT"},
new Employee{ID= 108,Name = "Manoj", Salary = 11000, Department = "Sales"},
new Employee{ID= 109,Name = "Pradeep", Salary = 20000, Department = "Sales"},
new Employee{ID= 110,Name = "Saurav", Salary = 25000, Department = "Sales"}
};
return listEmployees;
}
}
public static void Main()
{
//Using Method Syntax
var MethodSyntaxMax = Employee.GetAllEmployees().Max(x=> x.Salary);
var MethodSyntaxMin = Employee.GetAllEmployees().Min(x=> x.Salary);
//Using Query Syntax
var QuerySyntaxMax = (from emp in Employee.GetAllEmployees()
select emp).Max(x => x.Salary);
var QuerySyntaxMin = (from emp in Employee.GetAllEmployees()
select emp).Min(x => x.Salary);
Console.WriteLine($"Highest Salary by Method Syntax:{MethodSyntaxMax}");
Console.WriteLine($"Lowest Salary by Method Syntax:{MethodSyntaxMin}");
Console.WriteLine($"Highest Salary by Query Syntax:{QuerySyntaxMax}");
Console.WriteLine($"Lowest Salary by Query Syntax:{QuerySyntaxMin}");
Console.ReadKey();
}
}
Example 3
using System;
using System.Collections.Generic;
using System.Linq;
public class Program
{
public class Employee
{
public int ID { get; set; }
public string Name { get; set; }
public int Salary { get; set; }
public string Department { get; set; }
public static List<Employee> GetAllEmployees()
{
List<Employee> listEmployees = new List<Employee>()
{
new Employee{ID= 101,Name = "Pooja", Salary = 15000, Department = "IT"},
new Employee{ID= 102,Name = "Priyanka", Salary = 15000, Department = "Sales"},
new Employee{ID= 103,Name = "Manoj", Salary = 25000, Department = "Sales"},
new Employee{ID= 104,Name = "Santosh", Salary = 20000, Department = "IT"},
new Employee{ID= 105,Name = "Vishal", Salary = 30000, Department = "IT"},
new Employee{ID= 106,Name = "Sandhya", Salary = 25000, Department = "IT"},
new Employee{ID= 107,Name = "Mahesh", Salary = 35000, Department = "IT"},
new Employee{ID= 108,Name = "Manoj", Salary = 11000, Department = "Sales"},
new Employee{ID= 109,Name = "Pradeep", Salary = 20000, Department = "Sales"},
new Employee{ID= 110,Name = "Saurav", Salary = 25000, Department = "Sales"}
};
return listEmployees;
}
}
public static void Main()
{
//Using Method Syntax
var MethodSyntaxMax = Employee.GetAllEmployees().Where(whr=> whr.Department == "IT").Max(x=> x.Salary);
var MethodSyntaxMin = Employee.GetAllEmployees().Where(whr=> whr.Department == "IT").Min(x=> x.Salary);
//Using Query Syntax
var QuerySyntaxMax = (from emp in Employee.GetAllEmployees()
where emp.Department == "IT"
select emp).Max(x => x.Salary);
var QuerySyntaxMin = (from emp in Employee.GetAllEmployees()
where emp.Department == "IT"
select emp).Min(x => x.Salary);
Console.WriteLine($"Highest Salary of Employye in IT Department by Method Syntax:{MethodSyntaxMax}");
Console.WriteLine($"Lowest Salary of Employye in IT Department by Method Syntax:{MethodSyntaxMin}");
Console.WriteLine($"Highest Salary of Employye in IT Department by Query Syntax:{QuerySyntaxMax}");
Console.WriteLine($"Lowest Salary of Employye in IT Department by Query Syntax:{QuerySyntaxMin}");
Console.ReadKey();
}
}
Sum(): It is used to calculate the total or sum of numeric values in the collection.
Example 1
using System;
using System.Collections.Generic;
using System.Linq;
public class Program
{
public static void Main()
{
int[] intList = new int[] { 10, 45, 35, 29, 100, 69, 58, 50 };
//Using Method Syntax
var MethodSyntax = intList.Sum();
//Using Query Syntax
var QuerySyntax = (from num in intList select num).Sum();
Console.WriteLine($"Sum of Numbers by Method Syntax: {MethodSyntax}");
Console.WriteLine($"Sum of Numbers by Query Syntax: {QuerySyntax}");
Console.ReadKey();
}
}
Example 2
using System;
using System.Collections.Generic;
using System.Linq;
public class Program
{
public class Employee
{
public int ID { get; set; }
public string Name { get; set; }
public int Salary { get; set; }
public string Department { get; set; }
public static List<Employee> GetAllEmployees()
{
List<Employee> listEmployees = new List<Employee>()
{
new Employee{ID= 101,Name = "Pooja", Salary = 15000, Department = "IT"},
new Employee{ID= 102,Name = "Priyanka", Salary = 15000, Department = "Sales"},
new Employee{ID= 103,Name = "Manoj", Salary = 25000, Department = "Sales"},
new Employee{ID= 104,Name = "Santosh", Salary = 20000, Department = "IT"},
new Employee{ID= 105,Name = "Vishal", Salary = 30000, Department = "IT"},
new Employee{ID= 106,Name = "Sandhya", Salary = 25000, Department = "IT"},
new Employee{ID= 107,Name = "Mahesh", Salary = 35000, Department = "IT"},
new Employee{ID= 108,Name = "Manoj", Salary = 11000, Department = "Sales"},
new Employee{ID= 109,Name = "Pradeep", Salary = 20000, Department = "Sales"},
new Employee{ID= 110,Name = "Saurav", Salary = 25000, Department = "Sales"}
};
return listEmployees;
}
}
public static void Main()
{
//Using Method Syntax
var MethodSyntax = Employee.GetAllEmployees().Sum(x=> x.Salary);
//Using Query Syntax
var QuerySyntax = (from emp in Employee.GetAllEmployees() select emp).Sum(x=> x.Salary);
Console.WriteLine($"Sum of Salary by Method Syntax: {MethodSyntax}");
Console.WriteLine($"Sum of Salary by Query Syntax: {QuerySyntax}");
Console.ReadKey();
}
}
Example 3
using System;
using System.Collections.Generic;
using System.Linq;
public class Program
{
public class Employee
{
public int ID { get; set; }
public string Name { get; set; }
public int Salary { get; set; }
public string Department { get; set; }
public static List<Employee> GetAllEmployees()
{
List<Employee> listEmployees = new List<Employee>()
{
new Employee{ID= 101,Name = "Pooja", Salary = 15000, Department = "IT"},
new Employee{ID= 102,Name = "Priyanka", Salary = 15000, Department = "Sales"},
new Employee{ID= 103,Name = "Manoj", Salary = 25000, Department = "Sales"},
new Employee{ID= 104,Name = "Santosh", Salary = 20000, Department = "IT"},
new Employee{ID= 105,Name = "Vishal", Salary = 30000, Department = "IT"},
new Employee{ID= 106,Name = "Sandhya", Salary = 25000, Department = "IT"},
new Employee{ID= 107,Name = "Mahesh", Salary = 35000, Department = "IT"},
new Employee{ID= 108,Name = "Manoj", Salary = 11000, Department = "Sales"},
new Employee{ID= 109,Name = "Pradeep", Salary = 20000, Department = "Sales"},
new Employee{ID= 110,Name = "Saurav", Salary = 25000, Department = "Sales"}
};
return listEmployees;
}
}
public static void Main()
{
//Using Method Syntax
var MethodSyntax = Employee.GetAllEmployees().Where(whr=> whr.Department == "IT").Sum(x=> x.Salary);
//Using Query Syntax
var QuerySyntax = (from emp in Employee.GetAllEmployees() where emp.Department =="Sales" select emp).Sum(x=> x.Salary);
Console.WriteLine($"Sum of Salary of IT Department by Method Syntax: {MethodSyntax}");
Console.WriteLine($"Sum of Salary of Sales Department by Query Syntax: {QuerySyntax}");
Console.ReadKey();
}
}
Skip(): It is used to skip or bypass the first n number of elements from a data source or sequence and then returns the remaining elements from the data source as output.
Example 1
using System;
using System.Collections.Generic;
using System.Linq;
public class Program
{
public static void Main()
{
int[] intList = new int[] { 10, 45, 35, 29, 100, 69, 58, 50 };
//Using Method Syntax
var MethodSyntax = intList.Skip(4).ToList();
//Using Query Syntax
var QuerySyntax = (from num in intList select num).Skip(4).ToList();
Console.WriteLine($"Method Syntax");
foreach (var num in MethodSyntax)
{
Console.WriteLine($"{num}");
}
Console.WriteLine($"Query Syntax");
foreach (var num in QuerySyntax)
{
Console.WriteLine($"{num}");
}
Console.ReadKey();
}
}
Example 2
using System;
using System.Collections.Generic;
using System.Linq;
public class Program
{
public class Employee
{
public int ID { get; set; }
public string Name { get; set; }
public int Salary { get; set; }
public string Department { get; set; }
public static List<Employee> GetAllEmployees()
{
List<Employee> listEmployees = new List<Employee>()
{
new Employee{ID= 101,Name = "Pooja", Salary = 15000, Department = "IT"},
new Employee{ID= 102,Name = "Priyanka", Salary = 15000, Department = "Sales"},
new Employee{ID= 103,Name = "Manoj", Salary = 25000, Department = "Sales"},
new Employee{ID= 104,Name = "Santosh", Salary = 20000, Department = "IT"},
new Employee{ID= 105,Name = "Vishal", Salary = 30000, Department = "IT"},
new Employee{ID= 106,Name = "Sandhya", Salary = 25000, Department = "IT"},
new Employee{ID= 107,Name = "Mahesh", Salary = 35000, Department = "IT"},
new Employee{ID= 108,Name = "Manoj", Salary = 11000, Department = "Sales"},
new Employee{ID= 109,Name = "Pradeep", Salary = 20000, Department = "Sales"},
new Employee{ID= 110,Name = "Saurav", Salary = 25000, Department = "Sales"}
};
return listEmployees;
}
}
public static void Main()
{
//Skip the First Two Elements and Return Remaining Elements from the list
//Using Method Syntax
var MethodSyntax = Employee.GetAllEmployees().Where(whr=> whr.Department == "IT").Skip(2).ToList();
//Using Query Syntax
var QuerySyntax = (from emp in Employee.GetAllEmployees() where emp.Department =="Sales" select emp).Skip(2).ToList();
Console.WriteLine($"Method Syntax");
foreach (var item in MethodSyntax)
{
Console.WriteLine($"ID:{item.ID} Name:{item.Name} Department:{item.Department} Salary:{item.Salary}");
}
Console.WriteLine($"Query Syntax");
foreach (var item in QuerySyntax)
{
Console.WriteLine($"ID:{item.ID} Name:{item.Name} Department:{item.Department} Salary:{item.Salary}");
}
Console.ReadKey();
}
}
SkipWhile(): It is used to skip all the elements from a data source or a sequence.
Example 1
using System;
using System.Collections.Generic;
using System.Linq;
public class Program
{
public static void Main()
{
int[] intList = new int[] { 10, 45, 35, 29, 100, 69, 58, 50 };
//Using Method Syntax
var MethodSyntax = intList.SkipWhile(num => num < 40).ToList();
//Using Query Syntax
var QuerySyntax = (from num in intList select num).SkipWhile(num => num < 40).ToList();
Console.WriteLine($"Method Syntax");
foreach (var num in MethodSyntax)
{
Console.WriteLine($"{num} ");
}
Console.WriteLine($"Query Syntax");
foreach (var num in QuerySyntax)
{
Console.WriteLine($"{num} ");
}
Console.ReadKey();
}
}
Example 2
using System;
using System.Collections.Generic;
using System.Linq;
public class Program
{
public class Employee
{
public int ID { get; set; }
public string Name { get; set; }
public int Salary { get; set; }
public string Department { get; set; }
public static List<Employee> GetAllEmployees()
{
List<Employee> listEmployees = new List<Employee>()
{
new Employee{ID= 101,Name = "Pooja", Salary = 15000, Department = "IT"},
new Employee{ID= 102,Name = "Priyanka", Salary = 15000, Department = "Sales"},
new Employee{ID= 103,Name = "Manoj", Salary = 25000, Department = "Sales"},
new Employee{ID= 104,Name = "Santosh", Salary = 20000, Department = "IT"},
new Employee{ID= 105,Name = "Vishal", Salary = 30000, Department = "IT"},
new Employee{ID= 106,Name = "Sandhya", Salary = 25000, Department = "IT"},
new Employee{ID= 107,Name = "Mahesh", Salary = 35000, Department = "IT"},
new Employee{ID= 108,Name = "Manoj", Salary = 11000, Department = "Sales"},
new Employee{ID= 109,Name = "Pradeep", Salary = 20000, Department = "Sales"},
new Employee{ID= 110,Name = "Saurav", Salary = 25000, Department = "Sales"}
};
return listEmployees;
}
}
public static void Main()
{
//Using Method Syntax
var MethodSyntax = Employee.GetAllEmployees().Where(whr=> whr.Department == "IT").SkipWhile(x => x.Name.Length < 6).ToList();
Console.WriteLine($"Method Syntax");
foreach (var item in MethodSyntax)
{
Console.WriteLine($"{item.Name} ");
}
Console.ReadKey();
}
}
Take(): It is used to fetch the first "n" number of elements from the data source, where "n" is an integer that is passed as a parameter to the LINQ Take method.
Example 1
using System;
using System.Linq;
public class Program
{
public static void Main()
{
int[] IntArray = { 10, 30, 50, 40, 20, 60 };
//Using Method Syntax
var MethodSyntax = IntArray.Take(3).ToList();
//Using Query Syntax
var QuerySyntax = (from num in IntArray select num).Take(3).ToList();
Console.WriteLine("After Sorting Method Syntax");
foreach (var item in MethodSyntax)
{
Console.Write($"{item} ");
}
Console.WriteLine("After Sorting Query Syntax");
foreach (var item in QuerySyntax)
{
Console.Write($"{item} ");
}
}
}
Example 2
using System;
using System.Linq;
using System.Collections.Generic;
public class Program
{
public class Employee
{
public int ID { get; set; }
public string Name { get; set; }
public int Salary { get; set; }
public string Department { get; set; }
public static List<Employee> GetAllEmployees()
{
List<Employee> listEmployees = new List<Employee>()
{
new Employee{ID= 101,Name = "Pooja", Salary = 15000, Department = "IT"},
new Employee{ID= 102,Name = "Priyanka", Salary = 15000, Department = "Sales"},
new Employee{ID= 103,Name = "Manoj", Salary = 25000, Department = "Sales"},
new Employee{ID= 104,Name = "Santosh", Salary = 20000, Department = "IT"},
new Employee{ID= 105,Name = "Vishal", Salary = 30000, Department = "IT"},
new Employee{ID= 106,Name = "Sandhya", Salary = 25000, Department = "IT"},
new Employee{ID= 107,Name = "Mahesh", Salary = 35000, Department = "IT"},
new Employee{ID= 108,Name = "Manoj", Salary = 11000, Department = "Sales"},
new Employee{ID= 109,Name = "Pradeep", Salary = 20000, Department = "Sales"},
new Employee{ID= 110,Name = "Saurav", Salary = 25000, Department = "Sales"}
};
return listEmployees;
}
}
public static void Main()
{
//Using Method Syntax
var MethodSyntax = Employee.GetAllEmployees().Where(whr=> whr.Department == "IT").Take(3).ToList();
//Using Query Syntax
var QuerySyntax = (from emp in Employee.GetAllEmployees() where emp.Department == "Sales" select emp).Take(3).ToList();
Console.WriteLine("Method Syntax");
foreach (var item in MethodSyntax)
{
Console.Write($"Department: {item.Department} Name: {item.Name} ");
}
Console.WriteLine("Query Syntax");
foreach (var item in QuerySyntax)
{
Console.Write($"Department: {item.Department} Name: {item.Name} ");
}
Console.ReadKey();
}
}
TakeWhile(): It is used to fetch all the elements from a data source or a sequence, or a collection until a specified condition is true.
Example 1
using System;
using System.Collections.Generic;
using System.Linq;
public class Program
{
public static void Main()
{
int[] intList = new int[] { 10, 45, 35, 29, 100, 69, 58, 50 };
//Using Method Syntax
var MethodSyntax = intList.TakeWhile(num => num < 40).ToList();
//Using Query Syntax
var QuerySyntax = (from num in intList select num).TakeWhile(num => num < 40).ToList();
Console.WriteLine($"Method Syntax");
foreach (var num in MethodSyntax)
{
Console.WriteLine($"{num} ");
}
Console.WriteLine($"Query Syntax");
foreach (var num in QuerySyntax)
{
Console.WriteLine($"{num} ");
}
Console.ReadKey();
}
}
Example 2
using System;
using System.Collections.Generic;
using System.Linq;
public class Program
{
public class Employee
{
public int ID { get; set; }
public string Name { get; set; }
public int Salary { get; set; }
public string Department { get; set; }
public static List<Employee> GetAllEmployees()
{
List<Employee> listEmployees = new List<Employee>()
{
new Employee{ID= 101,Name = "Pooja", Salary = 15000, Department = "IT"},
new Employee{ID= 102,Name = "Priyanka", Salary = 15000, Department = "Sales"},
new Employee{ID= 103,Name = "Manoj", Salary = 25000, Department = "Sales"},
new Employee{ID= 104,Name = "Santosh", Salary = 20000, Department = "IT"},
new Employee{ID= 105,Name = "Vishal", Salary = 30000, Department = "IT"},
new Employee{ID= 106,Name = "Sandhya", Salary = 25000, Department = "IT"},
new Employee{ID= 107,Name = "Mahesh", Salary = 35000, Department = "IT"},
new Employee{ID= 108,Name = "Manoj", Salary = 11000, Department = "Sales"},
new Employee{ID= 109,Name = "Pradeep", Salary = 20000, Department = "Sales"},
new Employee{ID= 110,Name = "Saurav", Salary = 25000, Department = "Sales"}
};
return listEmployees;
}
}
public static void Main()
{
//Using Method Syntax
var MethodSyntax = Employee.GetAllEmployees().TakeWhile(x => x.Name.Length > 4).ToList();
Console.WriteLine($"Method Syntax");
foreach (var item in MethodSyntax)
{
Console.WriteLine($"{item.Name} ");
}
Console.ReadKey();
}
}
Concat(): It is used to concatenate two sequences into one sequence.
Example 1
using System;
using System.Linq;
using System.Collections.Generic;
public class Program
{
public static void Main()
{
int[] IntArray = { 10, 30, 50, 40, 20, 60 };
int[] IntArray2 = { 70, 80, 5, 90, 100 };
//Using Method Syntax
var MethodSyntax = IntArray.Concat(IntArray2);
//Using Query Syntax
var QuerySyntax = (from num in IntArray select num).Concat(IntArray2);
Console.WriteLine("Method Syntax");
foreach (var item in MethodSyntax)
{
Console.Write($"{item} ");
}
Console.WriteLine("Query Syntax");
foreach (var item in QuerySyntax)
{
Console.Write($"{item} ");
}
Console.ReadKey();
}
}
Example 2
using System;
using System.Linq;
using System.Collections.Generic;
public class Program
{
public class Employee
{
public int ID { get; set; }
public string Name { get; set; }
public int Salary { get; set; }
public string Department { get; set; }
public static List<Employee> GetAllEmployees()
{
List<Employee> listEmployees = new List<Employee>()
{
new Employee{ID= 101,Name = "Pooja", Salary = 15000, Department = "IT"},
new Employee{ID= 102,Name = "Priyanka", Salary = 15000, Department = "Sales"},
new Employee{ID= 103,Name = "Manoj", Salary = 25000, Department = "Sales"},
new Employee{ID= 104,Name = "Santosh", Salary = 20000, Department = "IT"},
new Employee{ID= 105,Name = "Vishal", Salary = 30000, Department = "IT"},
new Employee{ID= 106,Name = "Sandhya", Salary = 25000, Department = "IT"},
new Employee{ID= 107,Name = "Mahesh", Salary = 35000, Department = "IT"},
new Employee{ID= 108,Name = "Manoj", Salary = 11000, Department = "Sales"},
new Employee{ID= 109,Name = "Pradeep", Salary = 20000, Department = "Sales"},
new Employee{ID= 110,Name = "Saurav", Salary = 25000, Department = "Sales"}
};
return listEmployees;
}
public static List<Employee> GetEmployees2()
{
List<Employee> listEmployees = new List<Employee>()
{
new Employee{ID= 101,Name = "Raj", Salary = 15000, Department = "IT"},
new Employee{ID= 102,Name = "Priya", Salary = 15000, Department = "Sales"},
new Employee{ID= 103,Name = "Sandeep", Salary = 25000, Department = "Sales"},
new Employee{ID= 104,Name = "Shradha", Salary = 25000, Department = "IT"},
};
return listEmployees;
}
}
public static void Main()
{
//Using Method Syntax
var MethodSyntax = Employee.GetAllEmployees().Concat(Employee.GetEmployees2()).ToList();
//Using Query Syntax
var QuerySyntax = (from emp in Employee.GetAllEmployees() select emp).Concat(Employee.GetEmployees2()).ToList();
Console.WriteLine("Method Syntax");
foreach (var item in MethodSyntax)
{
Console.Write($"Name: {item.Name} ");
}
Console.WriteLine("Query Syntax");
foreach (var item in QuerySyntax)
{
Console.Write($"Name: {item.Name} ");
}
Console.ReadKey();
}
}
Union(): It is used to combine multiple data sources into one data source by removing duplicate elements.
Example 1
using System;
using System.Linq;
using System.Collections.Generic;
public class Program
{
public static void Main()
{
int[] IntArray = { 10, 30, 50, 40, 20, 60 };
int[] IntArray2 = { 30, 10, 5, 90, 100 };
//Using Method Syntax
var MethodSyntax = IntArray.Union(IntArray2);
//Using Query Syntax
var QuerySyntax = (from num in IntArray select num).Union(IntArray2);
Console.WriteLine("Method Syntax");
foreach (var item in MethodSyntax)
{
Console.Write($"{item} ");
}
Console.WriteLine("Query Syntax");
foreach (var item in QuerySyntax)
{
Console.Write($"{item} ");
}
Console.ReadKey();
}
}
Example 2
using System;
using System.Linq;
using System.Collections.Generic;
public class Program
{
public class Employee
{
public int ID { get; set; }
public string Name { get; set; }
public int Salary { get; set; }
public string Department { get; set; }
public static List<Employee> GetAllEmployees()
{
List<Employee> listEmployees = new List<Employee>()
{
new Employee{ID= 101,Name = "Pooja", Salary = 15000, Department = "IT"},
new Employee{ID= 102,Name = "Priyanka", Salary = 15000, Department = "Sales"},
new Employee{ID= 103,Name = "Manoj", Salary = 25000, Department = "Sales"},
new Employee{ID= 104,Name = "Santosh", Salary = 20000, Department = "IT"},
new Employee{ID= 105,Name = "Vishal", Salary = 30000, Department = "IT"},
new Employee{ID= 106,Name = "Sandhya", Salary = 25000, Department = "IT"},
new Employee{ID= 107,Name = "Mahesh", Salary = 35000, Department = "IT"},
new Employee{ID= 108,Name = "Manoj", Salary = 11000, Department = "Sales"},
new Employee{ID= 109,Name = "Pradeep", Salary = 20000, Department = "Sales"},
new Employee{ID= 110,Name = "Saurav", Salary = 25000, Department = "Sales"}
};
return listEmployees;
}
public static List<Employee> GetEmployees2()
{
List<Employee> listEmployees = new List<Employee>()
{
new Employee{ID= 101,Name = "Vishal", Salary = 15000, Department = "IT"},
new Employee{ID= 102,Name = "Priya", Salary = 15000, Department = "Sales"},
new Employee{ID= 103,Name = "Mahesh", Salary = 25000, Department = "Sales"},
new Employee{ID= 104,Name = "Shradha", Salary = 25000, Department = "IT"},
};
return listEmployees;
}
}
public static void Main()
{
//Using Method Syntax
var MethodSyntax = Employee.GetAllEmployees().Union(Employee.GetEmployees2()).ToList();
//Using Query Syntax
var QuerySyntax = (from emp in Employee.GetAllEmployees() select emp).Union(Employee.GetEmployees2()).ToList();
Console.WriteLine("Method Syntax");
foreach (var item in MethodSyntax)
{
Console.Write($"Name: {item.Name} ");
}
Console.WriteLine("Query Syntax");
foreach (var item in QuerySyntax)
{
Console.Write($"Name: {item.Name} ");
}
Console.ReadKey();
}
}
Count(): It is used to return the number of elements present in the collection or the number of elements that have satisfied a given condition.
Example 1
using System;
using System.Linq;
using System.Collections.Generic;
public class Program
{
public static void Main()
{
int[] IntArray = { 10, 30, 50, 40, 90, 20, 60, 70, 80, 100 };
//Using Method Syntax
var MethodSyntax = IntArray.Count();
//Using Query Syntax
var QuerySyntax = (from num in IntArray select num).Count();
Console.WriteLine(MethodSyntax);
Console.WriteLine(QuerySyntax);
Console.ReadKey();
}
}
Example 2
using System;
using System.Linq;
using System.Collections.Generic;
public class Program
{
public static void Main()
{
int[] IntArray = { 10, 30, 50, 40, 90, 20, 60, 70, 80, 100 };
//Using Method Syntax
var MethodSyntax = IntArray.Where(whr => whr > 40).Count();
//Using Query Syntax
var QuerySyntax = (from num in IntArray where num > 50 select num).Count();
Console.WriteLine(MethodSyntax);
Console.WriteLine(QuerySyntax);
Console.ReadKey();
}
}
Example 3
using System;
using System.Linq;
using System.Collections.Generic;
public class Program
{
public class Employee
{
public int ID { get; set; }
public string Name { get; set; }
public int Salary { get; set; }
public string Department { get; set; }
public static List<Employee> GetAllEmployees()
{
List<Employee> listEmployees = new List<Employee>()
{
new Employee{ID= 101,Name = "Pooja", Salary = 15000, Department = "IT"},
new Employee{ID= 102,Name = "Priyanka", Salary = 15000, Department = "Sales"},
new Employee{ID= 103,Name = "Manoj", Salary = 25000, Department = "Sales"},
new Employee{ID= 104,Name = "Santosh", Salary = 20000, Department = "IT"},
new Employee{ID= 105,Name = "Vishal", Salary = 30000, Department = "IT"},
new Employee{ID= 106,Name = "Sandhya", Salary = 25000, Department = "IT"},
new Employee{ID= 107,Name = "Mahesh", Salary = 35000, Department = "IT"},
new Employee{ID= 108,Name = "Manoj", Salary = 11000, Department = "Sales"},
new Employee{ID= 109,Name = "Pradeep", Salary = 20000, Department = "Sales"},
new Employee{ID= 110,Name = "Saurav", Salary = 25000, Department = "Sales"}
};
return listEmployees;
}
}
public static void Main()
{
//Using Method Syntax
var MethodSyntax = Employee.GetAllEmployees().Where(whr => whr.Salary > 25000).Count();
//Using Query Syntax
var QuerySyntax = (from emp in Employee.GetAllEmployees() where emp.Department == "IT" select emp).Count();
Console.WriteLine(MethodSyntax);
Console.WriteLine(QuerySyntax);
Console.ReadKey();
}
}
Range(): It is used to Generate a sequence of integral (integer) numbers within a specified range.
Example
using System;
using System.Linq;
public class Program
{
public static void Main()
{
// Generating Intger Numbers from 1 to 10
var MethodSyntax = Enumerable.Range(1, 10);
foreach (var item in MethodSyntax )
{
Console.WriteLine(item);
}
Console.ReadKey();
}
}
ToLookup(): It is used for grouping data based on key/value pair.
Example
using System;
using System.Linq;
using System.Collections.Generic;
public class Program
{
public class Employee
{
public int ID { get; set; }
public string Name { get; set; }
public int Salary { get; set; }
public string Department { get; set; }
public static List<Employee> GetAllEmployees()
{
List<Employee> listEmployees = new List<Employee>()
{
new Employee{ID= 101,Name = "Pooja", Salary = 15000, Department = "IT"},
new Employee{ID= 102,Name = "Priyanka", Salary = 15000, Department = "Sales"},
new Employee{ID= 103,Name = "Manoj", Salary = 25000, Department = "Sales"},
new Employee{ID= 104,Name = "Santosh", Salary = 20000, Department = "IT"},
new Employee{ID= 105,Name = "Vishal", Salary = 30000, Department = "IT"},
new Employee{ID= 106,Name = "Sandhya", Salary = 25000, Department = "IT"},
new Employee{ID= 107,Name = "Mahesh", Salary = 35000, Department = "IT"},
new Employee{ID= 108,Name = "Manoj", Salary = 11000, Department = "Sales"},
new Employee{ID= 109,Name = "Pradeep", Salary = 20000, Department = "Sales"},
new Employee{ID= 110,Name = "Saurav", Salary = 25000, Department = "Sales"}
};
return listEmployees;
}
}
public static void Main()
{
//Using Method Syntax
var MethodSyntax = Employee.GetAllEmployees().ToLookup(x => x.Department);
//Using Query Syntax
var QuerySyntax = (from emp in Employee.GetAllEmployees() select emp).ToLookup(x => x.Department);
Console.WriteLine("Method Syntax");
foreach(var grp in MethodSyntax)
{
Console.WriteLine($"{grp.Key} : {grp.Count()}");
foreach (var item in grp)
{
Console.Write($"Name: {item.Name} Salary: {item.Salary} ");
}
}
Console.WriteLine("Query Syntax");
foreach(var grp in QuerySyntax)
{
Console.WriteLine($"{grp.Key} : {grp.Count()}");
foreach (var item in grp)
{
Console.Write($"Name: {item.Name} Salary: {item.Salary} ");
}
}
Console.ReadKey();
}
}
Intersect(): It is used to return the common elements from both collections.
Example
using System;
using System.Linq;
using System.Collections.Generic;
public class Program
{
public static void Main()
{
int[] IntArray = { 10, 30, 50, 40, 90, 20, 60, 70, 80, 100 };
int[] IntArray2 = { 10, 30, 40, 55, 75, 99, 100 };
//Using Method Syntax
var MethodSyntax = IntArray.Intersect(IntArray2).ToList();
//Using Query Syntax
var QuerySyntax = (from num in IntArray select num).Intersect(IntArray2).ToList();
foreach (var item in MethodSyntax)
{
Console.WriteLine(item);
}
foreach (var item in QuerySyntax)
{
Console.WriteLine(item);
}
Console.ReadKey();
}
}
Except(): It is used to return the elements which are present in the first data source but not in the second data source.
Example
using System;
using System.Linq;
using System.Collections.Generic;
public class Program
{
public static void Main()
{
int[] IntArray = { 10, 30, 50, 40, 90, 20, 60, 70, 80, 100 };
int[] IntArray2 = { 10, 30, 40, 55, 75, 99, 100 };
//Using Method Syntax
var MethodSyntax = IntArray.Except(IntArray2).ToList();
//Using Query Syntax
var QuerySyntax = (from num in IntArray select num).Except(IntArray2).ToList();
foreach (var item in MethodSyntax)
{
Console.WriteLine(item);
}
foreach (var item in QuerySyntax)
{
Console.WriteLine(item);
}
Console.ReadKey();
}
}
Repeat(): It is used to generate a sequence or a collection with a specified number of elements, and each element contains the same value.
Example
using System;
using System.Linq;
using System.Collections.Generic;
public class Program
{
public static void Main()
{
//Repeating the string value Welcome to DOT NET Tutorials for 10 Times
//Using the Repeat Method
var MethodSyntax = Enumerable.Repeat("Vishal Yelve", 5);
foreach (var item in MethodSyntax)
{
Console.WriteLine(item);
}
Console.ReadKey();
}
}
OfType(): It is used to filter specific type data from a data source based on the data type we passed to this operator.
Example 1
using System;
using System.Linq;
using System.Collections.Generic;
public class Program
{
public static void Main()
{
var data = new List<object>()
{
"Vishal", "Sandhya", 15000, "Mahesh", "Pooja", 10000, 20000, 30000, 40000, "Santosh"
};
//Using Method Syntax
var MethodSyntax =data.OfType<string>().ToList();
//Using Query Syntax
var QuerySyntax = (from emp in data.OfType<int>() select emp);
foreach (var item in MethodSyntax)
{
Console.WriteLine(item);
}
foreach (var item in QuerySyntax)
{
Console.WriteLine(item);
}
Console.ReadKey();
}
}
Example 2
using System;
using System.Linq;
using System.Collections.Generic;
public class Program
{
public class Employee
{
public int ID { get; set; }
public string Name { get; set; }
public int Salary { get; set; }
public string Department { get; set; }
public static List<Employee> GetAllEmployees()
{
List<Employee> listEmployees = new List<Employee>()
{
new Employee{ID= 101,Name = "Pooja", Salary = 15000, Department = "IT"},
new Employee{ID= 102,Name = "Priyanka", Salary = 15000, Department = "Sales"},
new Employee{ID= 103,Name = "Manoj", Salary = 25000, Department = "Sales"},
new Employee{ID= 104,Name = "Santosh", Salary = 20000, Department = "IT"},
new Employee{ID= 105,Name = "Vishal", Salary = 30000, Department = "IT"},
new Employee{ID= 106,Name = "Sandhya", Salary = 25000, Department = "IT"},
new Employee{ID= 107,Name = "Mahesh", Salary = 35000, Department = "IT"},
new Employee{ID= 108,Name = "Manoj", Salary = 11000, Department = "Sales"},
new Employee{ID= 109,Name = "Pradeep", Salary = 20000, Department = "Sales"},
new Employee{ID= 110,Name = "Saurav", Salary = 25000, Department = "Sales"}
};
return listEmployees;
}
}
public static void Main()
{
//Using Method Syntax
var MethodSyntax = Employee.GetAllEmployees().Where(whr => whr.Department == "IT") .OfType<Employee>().ToList();
//Using Query Syntax
var QuerySyntax = (from emp in Employee.GetAllEmployees().OfType<Employee>() where emp.Department == "Sales" select emp);
Console.WriteLine("Method Syntax");
foreach (var item in MethodSyntax)
{
Console.WriteLine($"Name: {item.Name} Salary: {item.Salary}");
}
Console.WriteLine("Query Syntax");
foreach (var item in QuerySyntax)
{
Console.WriteLine($"Name: {item.Name} Salary: {item.Salary}");
}
Console.ReadKey();
}
}
GroupJoin(): It is used to produce hierarchical data structures.
Example
using System;
using System.Linq;
using System.Collections.Generic;
public class Program
{
public class Employee
{
public int ID { get; set; }
public string Name { get; set; }
public int Salary { get; set; }
public int DepartmentId { get; set; }
public static List<Employee> GetAllEmployees()
{
List<Employee> listEmployees = new List<Employee>()
{
new Employee{ID= 101, Name = "Pooja", Salary = 15000, DepartmentId = 10},
new Employee{ID= 102, Name = "Priyanka", Salary = 15000, DepartmentId = 30},
new Employee{ID= 103, Name = "Manoj", Salary = 25000, DepartmentId = 20},
new Employee{ID= 104, Name = "Santosh", Salary = 20000, DepartmentId = 10},
new Employee{ID= 105, Name = "Vishal", Salary = 30000, DepartmentId = 10},
new Employee{ID= 106, Name = "Sandhya", Salary = 25000, DepartmentId = 10},
new Employee{ID= 107, Name = "Mahesh", Salary = 35000, DepartmentId = 30},
new Employee{ID= 108, Name = "Manoj", Salary = 11000, DepartmentId = 10},
new Employee{ID= 109, Name = "Pradeep", Salary = 20000, DepartmentId = 20},
new Employee{ID= 110, Name = "Saurav", Salary = 25000, DepartmentId = 20}
};
return listEmployees;
}
}
public class Department
{
public int ID { get; set; }
public string Name { get; set; }
public static List<Department> GetAllDepartments()
{
return new List<Department>()
{
new Department { ID = 10, Name = "IT"},
new Department { ID = 20, Name = "HR"},
new Department { ID = 30, Name = "Sales"},
};
}
}
public static void Main()
{
//Group Employees by Department using Method Syntax
var groupJoinMethodSyntax = Department.GetAllDepartments().
GroupJoin(Employee.GetAllEmployees(),
dept => dept.ID,
emp => emp.DepartmentId,
(dept, emp) => new { dept, emp }
);
var GroupJoinQuerySyntax = (from dept in Department.GetAllDepartments()
join emp in Employee.GetAllEmployees() on dept.ID equals emp.DepartmentId
into EmployeeGroups
select new { dept, EmployeeGroups });
Console.WriteLine("Method Syntax");
foreach (var grp in groupJoinMethodSyntax)
{
Console.WriteLine($"Department: {grp.dept.Name}");
foreach (var item in grp.emp)
{
Console.WriteLine($"Name: {item.Name}");
}
}
Console.WriteLine("--------------------------- ");
Console.WriteLine("Query Syntax");
foreach (var grp in GroupJoinQuerySyntax)
{
Console.WriteLine($"Department: {grp.dept.Name}");
foreach (var item in grp.EmployeeGroups)
{
Console.WriteLine($"Name: {item.Name}");
}
}
Console.ReadKey();
}
}
Summary
In this article, I have tried to cover some of the important methods of LINQ, which are mostly used in our development.