Deferred query evaluation is a fundamental LINQ topic that you will learn in this article.
What exactly are LINQ Query Operators?
LINQ Query operators are a collection of extension methods that may be used to perform various actions in the context of LINQ queries. These operators contain the LINQ technology’s heart and soul, and they are the real elements that make LINQ possible.
The list of common query operators and their categories are as follows:
What is the definition of Deferred Query Evaluation?
Deferred query evaluation in LINQ means that the LINQ query is not evaluated when it is defined, but instead when it is used. Consider the query definition below.
List items = new List { "one", "two", "three" }; var allItems = from i in items select i; foreach (var s in allItems) { Console.WriteLine(s); } items.Add("four"); foreach (var s in allItems) { Console.WriteLine(s); }
The query is specified in the second line of the code snippet above. This will select all of the items and assign the results of the query to the variable all Items. You will get the following output if you execute the for each loop.
one two three
Then I added another item “four” to the list and ran all Items again without modifying the query, and the second loop returned the following output.
one two three four
The result is different because the query is not evaluated at the time of definition, but rather when iterating through the list of objects using the foreach loop. This means that you may define your query once and use it several times, even if the data in your source sequence has changed. In other words, each time you iterate your query, you will get new results.
A LINQ query, from a logical standpoint, defines a type of “query plan” that will not be performed until it is utilised and will be executed again each time it is run. Consider the following scenario: you want a secure copy of your query results that remains unchanged even if the source sequence changes. You may do this by using conversion operators like ToList or ToArray.
The most of the operators in the above table are deferred query operators, and they are easily identified since they all return IEnumerable or IOrderedEnumerable objects. In my project, I’m developing the following employee class with the basic characteristics to give you an overview of these operators and their examples.
class Employee { public int ID { get; set; } public string Name { get; set; } public decimal Salary { get; set; } }
I’m making the following list of objects in the main application. This list will be used as the input sequence in all of the query operator examples following.
List employees = new List(); employees.Add(new Employee { ID = 1, Name = "Peter", Salary = 10000 }); employees.Add(new Employee { ID = 2, Name = "David", Salary = 20000 }); employees.Add(new Employee { ID = 3, Name = "Simon", Salary = 25000 }); employees.Add(new Employee { ID = 4, Name = "John", Salary = 7000 }); employees.Add(new Employee { ID = 5, Name = "Alan", Salary = 3000 });
The rest of this course will provide examples of several of the deferred query operators available in LINQ. Because all query operators are defined as extension methods in the.NET Framework, I’m using extension method syntax in most of the examples below but also providing plain query syntax where applicable.
Where operator
To include just certain components in a sequence, use the Where operator. As an example, the following query may be used to filter out workers with salaries below $10,000.
Extension Method Syntax