Wednesday, September 27, 2017

Linq- Query Deferred Execution

LINQ queries have two different behaviors of execution
1. Deferred execution
2. Immediate execution
LINQ operators can be broadly classified into 2 categories based on the behavior of query execution
1. Deferred or Lazy Operators -  These query operators use deferred execution.
Examples - select, where, Take, Skip etc

2. Immediate or Greedy Operators - These query operators use immediate execution. 
Examples - count, average, min, max, ToList etc

Deferred Execution Example

// LINQ Query is only defined here and is not executed at this point
// If the query is executed at this point, the result should not display Tim
            IEnumerable<Student> result = from student in listStudents
                                          where student.TotalMarks == 800
                                          select student;

        // Add a new student object with TotalMarks = 800 to the source list
            listStudents.Add(new Student { StudentID = 104, Name = "Tim", TotalMarks = 800 });

       // The above query is actually executed when we iterate through the sequence
       // using the foreach loop. This is proved as Tim is also included in the result

            foreach (Student s in result)
            {
                Console.WriteLine(s.StudentID + "\t" + s.Name + "\t" + s.TotalMarks);
            }

Immediate Execution Example
// Since we are using ToList() which is a greedy operator
// the LINQ Query is executed immediately at this point
            IEnumerable<Student> result = (from student in listStudents
                                           where student.TotalMarks == 800
                                           select student).ToList();

            // Adding a new student object with TotalMarks = 800 to the source
            // will have no effect on the result as the query is already executed
            listStudents.Add(new Student { StudentID = 104, Name = "Tim", TotalMarks = 800 });

            // The above query is executed at the point where it is defined.
            // This is proved as Tim is not included in the result
            foreach (Student s in result)
            {
                Console.WriteLine(s.StudentID + "\t" + s.Name + "\t" + s.TotalMarks);
            }

Immediate Execution Example 2
// Since we are using Count() operator, the LINQ Query is executed at this point
            int result = (from student in listStudents
                          where student.TotalMarks == 800
                          select student).Count();

            // Adding a new student object with TotalMarks = 800 to the source
            // will have no effect on the result as the query is already executed
            listStudents.Add(new Student { StudentID = 104, Name = "Tim", TotalMarks = 800 });

            // The above query is executed at the point where it is defined.
            // This is proved as Tim is not included in the count
            Console.WriteLine("Students with Total Marks = 800 : " + result);
        }

0 comments:

Post a Comment

Twitter Delicious Facebook Digg Stumbleupon Favorites More