Deferred execution means that the evaluation of an expression is delayed until its realized value is actually required. It greatly improves performance by avoiding unnecessary execution.
Deferred execution is applicable on any in-memory collection as well as remote LINQ providers like LINQ-to-SQL, LINQ-to-Entities, LINQ-to-XML, etc.
Deferred execution means the expression will be excuted when the actual data is needed. It improves the peroformance because it reduces the query execution if it not needed.
Try this below code :
here the query is getting prepared in โ var filteredData = from d in datas where d.Id > 2 && d.Id < 5 select d; โ statement but is actually executed when we are doing the foreach of it.
using System;using System.Collections.Generic;using System.Linq;namespace test{ static class Test { public static void Main() { //Prepare the data for the query IList<Data> datas = new List<Data>(); datas.Add(new Data(1, "Data1", "Data1")); datas.Add(new Data(2, "Data1", "Data1")); datas.Add(new Data(3, "Data1", "Data1")); datas.Add(new Data(4, "Data1", "Data1")); datas.Add(new Data(5, "Data1", "Data1")); var filteredData = from d in datas where d.Id > 2 && d.Id < 5 select d; foreach (var item in filteredData) { Console.WriteLine(item.Id); Console.WriteLine(item.Test1); Console.WriteLine(item.Test2); } } } public class Data { public Data(int id, string test1, string test2) { Id = id; Test1 = test1; Test2 = test2; } public int Id { get; set; } public string Test1 { get; set; } public string Test2 { get; set; } }}
using System;
using System.Collections.Generic;
using System.Linq;
namespace test
{
static class Test
public static void Main()
//Prepare the data for the query
IList<Data> datas = new List<Data>();
datas.Add(new Data(1, "Data1", "Data1"));
datas.Add(new Data(2, "Data1", "Data1"));
datas.Add(new Data(3, "Data1", "Data1"));
datas.Add(new Data(4, "Data1", "Data1"));
datas.Add(new Data(5, "Data1", "Data1"));
var filteredData = from d in datas where d.Id > 2 && d.Id < 5 select d;
foreach (var item in filteredData)
Console.WriteLine(item.Id);
Console.WriteLine(item.Test1);
Console.WriteLine(item.Test2);
}
public class Data
public Data(int id, string test1, string test2)
Id = id;
Test1 = test1;
Test2 = test2;
public int Id { get; set; }
public string Test1 { get; set; }
public string Test2 { get; set; }
There is another way of implementing deferred using Yield keyword. See the below program same as above but using Yield
using System;using System.Collections.Generic;using System.Linq;namespace test{ static class Test { public static void Main() { //Prepare the data for the query IList<Data> datas = new List<Data>(); datas.Add(new Data(1, "Data1", "Data1")); datas.Add(new Data(2, "Data1", "Data1")); datas.Add(new Data(3, "Data1", "Data1")); datas.Add(new Data(4, "Data1", "Data1")); datas.Add(new Data(5, "Data1", "Data1")); var filteredData = from d in datas.getFilterData() select d; foreach (var item in filteredData) { Console.WriteLine(item.Id); Console.WriteLine(item.Test1); Console.WriteLine(item.Test2); } } public static IEnumerable<Data> getFilterData(this IEnumerable<Data> source) { foreach (var item in source) { if (item.Id > 2 && item.Id < 5) yield return item; } } } public class Data { public Data(int id, string test1, string test2) { Id = id; Test1 = test1; Test2 = test2; } public int Id { get; set; } public string Test1 { get; set; } public string Test2 { get; set; } }}
var filteredData = from d in datas.getFilterData() select d;
public static IEnumerable<Data> getFilterData(this IEnumerable<Data> source)
foreach (var item in source)
if (item.Id > 2 && item.Id < 5)
yield return item;