In above result we can realize that as the extension method method executed for each time when the respective(employeeMoreThan90ks) deferred query is executed, EmployeeMoreThan90k() is getting called when you iterate employeedetails using the foreach loop.This way we can create custom methods using the yield keyword to get the advantage of deferred execution.Deferred execution is important as it gives you the flexibility of constructing the query in several steps by separating query construction from query execution.
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace QueryExecutionInLINQ {
- class Program {
- static void Main(string[] args) {
- List < Employee > employeeDetails = new List < Employee > () {
- new Employee() {
- ID = 101, Name = "Gnanavel Sekar", Designation = "Software Engineer", Salary = 150000
- },
- new Employee() {
- ID = 102, Name = "Subash S", Designation = "Software Engineer", Salary = 100000
- },
- new Employee() {
- ID = 103, Name = "Robert S", Designation = "Software Engineer", Salary = 70000
- },
- new Employee() {
- ID = 103, Name = "Ammaiyappan K", Designation = "Software Devloper", Salary = 80000
- }
- };
- var moreThan90kEmployee = from result in employeeDetails
- where result.Salary > 90000
- select result;
- foreach(var item in moreThan90kEmployee) {
- Console.WriteLine("ID :" + item.ID + ", Name :" + item.Name + ", Designation :" + item.Designation + ", Salary :" + item.Salary);
- }
- employeeDetails.Add(new Employee {
- ID = 104, Name = "Ramar A", Designation = "Tech Lead", Salary = 200000
- });
- foreach(var item in moreThan90kEmployee) {
- Console.WriteLine("ID :" + item.ID + ", Name :" + item.Name + ", Designation :" + item.Designation + ", Salary :" + item.Salary);
- }
- var employeeMoreThan90k = from employee in employeeDetails.EmployeeMoreThan90k()
- select employee;
- foreach(Employee empDetail in employeeMoreThan90k) {
- Console.WriteLine("Name: {0}, Salary: {1}", empDetail.Name, empDetail.Salary);
- }
- }
- }
- public class Employee {
- public int ID {
- get;
- set;
- }
- public string Name {
- get;
- set;
- }
- public string Designation {
- get;
- set;
- }
- public double Salary {
- get;
- set;
- }
- }
- public static class ExtensionMethods {
- public static IEnumerable < Employee > EmployeeMoreThan90k(this IEnumerable < Employee > employee) {
- foreach(Employee emp in employee) {
- Console.WriteLine("Accessing Employee {0}", emp.Name);
- if (emp.Salary > 90000) yield
- return emp;
- }
- }
- }
- }
Use of Immediate query execution
Immediate query execution reverse of deferred query execution because it's forcing the linq to provide the immediate result and it always deals with the predefined extension method. We will see the example to get clear picture about this.
- List < Employee > employeeDetails = new List < Employee > () {
- new Employee() {
- ID = 101, Name = "Gnanavel Sekar", Designation = "Software Engineer", Salary = 150000
- },
- new Employee() {
- ID = 102, Name = "Subash S", Designation = "Software Engineer", Salary = 100000
- },
- new Employee() {
- ID = 103, Name = "Robert S", Designation = "Software Engineer", Salary = 70000
- },
- new Employee() {
- ID = 103, Name = "Ammaiyappan K", Designation = "Software Devloper", Salary = 80000
- }
- };
- var moreThan90kEmployees = (from result in employeeDetails where result.Salary > 90000 select result).ToList();
Same query I am writing here to get the employees who are more than 90k salary, here we can use the conversion operator(ToList(),ToArray(),etc) to get the immediate result, not only with conversion operator we can get the immediate result by using all aggregate function also.
For clear understanding see one more example,
- var moreThan90kEmployeeCount= (from result in employeeDetails;
- where result.Salary > 90000;
- select result).Count();
Result
In above example we got the immediate result of count who are more than 90k salary
Now we got the clear picture about immediate query execution, it's used to return the result immediately by using conversion operator, aggregate function, and more predefined functions.
Overall code for immediate query execution,
- List < Employee > employeeDetails = new List < Employee > () {
- new Employee() {
- ID = 101, Name = "Gnanavel Sekar", Designation = "Software Engineer", Salary = 150000
- },
- new Employee() {
- ID = 102, Name = "Subash S", Designation = "Software Engineer", Salary = 100000
- },
- new Employee() {
- ID = 103, Name = "Robert S", Designation = "Software Engineer", Salary = 70000
- },
- new Employee() {
- ID = 103, Name = "Ammaiyappan K", Designation = "Software Devloper", Salary = 80000
- }
- };
- var moreThan90kEmployees = (from result in employeeDetails where result.Salary > 90000 select result).ToList();
- var moreThan90kEmployeeCount = (from result in employeeDetails where result.Salary > 90000 select result).Count();
Difference between Deferred and Immediate Query execution
Moreover we seen both uses so we can easily list out.
Deferred Query Execution | Immediate Query Execution |
A query is not executed at the point of its declaration. It is executed when the Query variable is iterated by using loop like as for, foreach. | A query is executed at the point of its declaration. The query which returns a singleton value (a single value or a set of values) like Average, Sum, Count, List,etc |
It provides the facility of query reusability, since it always fetches the updated data from the data source which exists at the time of each execution. | It doesn't provide the facility of query re-usability since it always contains the same data which is fetched at the time of query declaration |
We can force a query to execute immediately by using the foreach or for. | We can force a query to execute immediately of by calling ToList, ToArray methods(conversion operators,aggregate operators) |
Summary
In this aricle we learned the use of deferred query execution, how to use the deferred query in custom extension methods and the use of immediate query execution and their differences.
I hope it was helpful, your comments and feeback are always welcome.