Interviews - C# Corner

Gajendra Jangid
What is Deferred Execution?
By Gajendra Jangid in .NET onJan 27 2018
  • Kalyan Mondal
    Jul, 2019 16

    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.

    1. using System;
    2. using System.Collections.Generic;
    3. using System.Linq;
    4. namespace test
    5. {
    6. static class Test
    7. {
    8. public static void Main()
    9. {
    10. //Prepare the data for the query
    11. IList<Data> datas = new List<Data>();
    12. datas.Add(new Data(1, "Data1", "Data1"));
    13. datas.Add(new Data(2, "Data1", "Data1"));
    14. datas.Add(new Data(3, "Data1", "Data1"));
    15. datas.Add(new Data(4, "Data1", "Data1"));
    16. datas.Add(new Data(5, "Data1", "Data1"));
    17. var filteredData = from d in datas where d.Id > 2 && d.Id < 5 select d;
    18. foreach (var item in filteredData)
    19. {
    20. Console.WriteLine(item.Id);
    21. Console.WriteLine(item.Test1);
    22. Console.WriteLine(item.Test2);
    23. }
    24. }
    25. }
    26. public class Data
    27. {
    28. public Data(int id, string test1, string test2)
    29. {
    30. Id = id;
    31. Test1 = test1;
    32. Test2 = test2;
    33. }
    34. public int Id { get; set; }
    35. public string Test1 { get; set; }
    36. public string Test2 { get; set; }
    37. }
    38. }

    There is another way of implementing deferred using Yield keyword. See the below program same as above but using Yield

    1. using System;
    2. using System.Collections.Generic;
    3. using System.Linq;
    4. namespace test
    5. {
    6. static class Test
    7. {
    8. public static void Main()
    9. {
    10. //Prepare the data for the query
    11. IList<Data> datas = new List<Data>();
    12. datas.Add(new Data(1, "Data1", "Data1"));
    13. datas.Add(new Data(2, "Data1", "Data1"));
    14. datas.Add(new Data(3, "Data1", "Data1"));
    15. datas.Add(new Data(4, "Data1", "Data1"));
    16. datas.Add(new Data(5, "Data1", "Data1"));
    17. var filteredData = from d in datas.getFilterData() select d;
    18. foreach (var item in filteredData)
    19. {
    20. Console.WriteLine(item.Id);
    21. Console.WriteLine(item.Test1);
    22. Console.WriteLine(item.Test2);
    23. }
    24. }
    25. public static IEnumerable<Data> getFilterData(this IEnumerable<Data> source)
    26. {
    27. foreach (var item in source)
    28. {
    29. if (item.Id > 2 && item.Id < 5)
    30. yield return item;
    31. }
    32. }
    33. }
    34. public class Data
    35. {
    36. public Data(int id, string test1, string test2)
    37. {
    38. Id = id;
    39. Test1 = test1;
    40. Test2 = test2;
    41. }
    42. public int Id { get; set; }
    43. public string Test1 { get; set; }
    44. public string Test2 { get; set; }
    45. }
    46. }

    • 0

Most Popular Companies

Most Popular Job Functions

MOST LIKED QUESTIONS