What is deferred execution vs. immediate execution in LINQ?
Submitted by: MuhammadIn LINQ, deferred execution simply means that the query is not executed at the time it is specified. Specifically, this is accomplished by assigning the query to a variable. When this is done, the query definition is stored in the variable but the query is not executed until the query variable is iterated over. For example:
DataContext productContext = new DataContext();
var productQuery = from product in productContext.Products
where product.Type == "SOAPS"
select product; // Query is NOT executed here
foreach (var product in productQuery) // Query executes HERE
{
Console.WriteLine(product.Name);
}
You can also force immediate execution of a query. This can be useful, for example, if the database is being updated frequently, and it is important in the logic of your program to ensure that the results you're accessing are those returned at the point in your code where the query was specified. Immediate execution is often forced using a method such as Average, Sum, Count, List, ToList, or ToArray. For example:
DataContext productContext = new DataContext();
var productCountQuery = (from product in productContext.Products
where product.Type == "SOAPS"
select product).Count(); // Query executes HERE
Submitted by: Muhammad
DataContext productContext = new DataContext();
var productQuery = from product in productContext.Products
where product.Type == "SOAPS"
select product; // Query is NOT executed here
foreach (var product in productQuery) // Query executes HERE
{
Console.WriteLine(product.Name);
}
You can also force immediate execution of a query. This can be useful, for example, if the database is being updated frequently, and it is important in the logic of your program to ensure that the results you're accessing are those returned at the point in your code where the query was specified. Immediate execution is often forced using a method such as Average, Sum, Count, List, ToList, or ToArray. For example:
DataContext productContext = new DataContext();
var productCountQuery = (from product in productContext.Products
where product.Type == "SOAPS"
select product).Count(); // Query executes HERE
Submitted by: Muhammad
Read Online Senior .Net Developer Job Interview Questions And Answers
Top Senior .Net Developer Questions
☺ | Do you know what is Garbage Collector? |
☺ | Do you know what is the difference between an abstract class and an interface? |
☺ | Please tell us what is the difference between Override and Overload in a method? |
☺ | Tell us why do we use the “using” statement? |
☺ | Do you know what is .NET Standard? |
Top Dot Net Technologies Categories
☺ | MSF Interview Questions. |
☺ | .Net Architecture Interview Questions. |
☺ | ASP.Net MVC Interview Questions. |
☺ | Entity Framework Interview Questions. |
☺ | C# (Sharp) Programming Language Interview Questions. |