Skip to main content

LINQ - Deferred and Immediate Execution


LINQ - Deferred and Immediate Execution


LINQ Queries can be categorised based on execution behaviour.


Deferred Execution

The execution of an expression is delayed until its realized actual value is required. Deferred execution can be applied on an in-memory collection as well as remote LINQ providers like LINQ to SQL, LINQ to XML etc. 


It greatly improves performance by avoiding unnecessary execution. Deferred execution reevaluates on each execution which is called LAZY Evaluation. It always gives the latest data of the collection when executing.


Deferred or Lazy Operators - These query operators use deferred execution.

Examples - Select, where, Take, Skip

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
List<Student> Students = new List<Student>
{
    new student() { Id = 1, Name = "Mike" },
    new student() { Id = 2, Name = "Gracy" },
    new student() { Id = 3, Name = "Mitch" }
};
var numbers = from x in list
                        where x.id > 1
                        select x;

foreach(Student s in Students)       --> Query Executed here
    Console.Writeline(s.Name);

Students.Add(new Student() { Id = 4, Name = John };

foreach(Student s in numbers)       --> Query Executed again here and returns the latest data
    Console.Writeline(s.Name);

Output:

Gracy
Mitch

Gracy
Mitch
John

Immediate Execution

Immediate or Greedy Operators - These query operators use immediate execution.

Examples - count, average, min, max, ToList

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
List<Student>; Students = new List<Student>;
{
    new student() { Id = 1, Name = "Chandru" },
    new student() { Id = 2, Name = "Swathi" },
    new student() { Id = 3, Name = "Sudhan" }
};
var numbers = (from x in list
                        where x.id > 1
                        select x).ToList();

Students.Add(new Student() { Id = 4, Name = Vellingiri };

foreach(Student s in numbers)       
Console.Writeline(s.Name);
//Doesnt reflect the newly added student since the query gets executed immediately

Output:
Swathi
Sudhan

Comments

Popular posts from this blog

How to clear Visual Studio Cache

How to clear visual studio cache Many times, during development you would face situations where project references are not loaded properly or you get missing/error DLL's. This is because the Component cache gets corrupted randomly and without any warnings. The first option that needs to be done is to clear component cache and restart Visual Studio since the Cache might be holding onto previous DLL versions. Here are the steps on how to clear Visual Studio Cache, Clearing Component Cache: Close all Visual Studio Instances running in your machine. Also, make sure devenv.exe is not running in the Task Manager Delete the Component cache directory - %USERPROFILE%\AppData\Local\Microsoft\VisualStudio\1x.0\ComponentModelCache Restart Visual Studio The above steps should fix the cache issue most of the times, but some times that is not enough and you need to perform the below steps as well. Clearing User's Temp Folder: Open the temp folder in this locatio n -  %USERPROFILE%\AppData\Loc...

How to dependency inject to static class

.Net core supports dependency injection. There are many ways that you can inject services like constructor injection, action method injection, property injection. But there will be scenarios where you need to inject dependency services to static classes. For example, injecting services to extension methods. First, create a static class with a one property IServiceProvider type public void ConfigureServices(IServiceCollection services) { services.AddScoped<ILoggerEntry, LoggerEntry>(); services.AddTransient<IMongoRepository, MongoRepository>(); } Second, configure your services in ConfigureServices() method in Startup.cs and define the lifetime of the service instance using either Transient, Scoped or Singleton types. public void ConfigureServices(IServiceCollection services) { services.AddScoped<ILoggerEntry, LoggerEntry>(); services.AddTransient<IMongoRepository, MongoRepository>(); } For the next step to configure the Static class provider proper...

Error NU1605 - Detected package downgrade. Reference the package directly from the project to select a different version.

Error NU1605 - Detected package downgrade This error occurs when a dependency package has a version higher than an existing package version in the project solution. Solution: Add the following in .csproj file < PackageReference > < NoWarn >$( NoWarn ); NU1605 </ NoWarn > </ PackageReference > Another way to do this is to right-click on the solution and  click  Properties . Click  Build  and under  Errors and warnings  add 1605 to the  SuppressWarnings  text box. You can also add multiple error codes that you want to suppress by adding each separated by a comma. P.S. The below screenshot is in VS2019 Mac Version