Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. LINQ
  4. Deferred execution and eager evaluation

Deferred execution and eager evaluation

Scheduled Pinned Locked Moved LINQ
csharplinqhelptutorialquestion
6 Posts 2 Posters 9 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    mbabube
    wrote on last edited by
    #1

    Hi Could you please give me an example for Deferred execution with eager evaluation in C#? I read from MSDN that deferred execution in LINQ can be implemented either with lazy or eager evaluation...i could find examples in the internet for Deferred execution with lazy evaluation ,however i could not find any example for Deferred execution with eager evaluation....please help me....its urgent...

    E 1 Reply Last reply
    0
    • M mbabube

      Hi Could you please give me an example for Deferred execution with eager evaluation in C#? I read from MSDN that deferred execution in LINQ can be implemented either with lazy or eager evaluation...i could find examples in the internet for Deferred execution with lazy evaluation ,however i could not find any example for Deferred execution with eager evaluation....please help me....its urgent...

      E Offline
      E Offline
      Eslam Afifi
      wrote on last edited by
      #2

      Lazy evaluation[^]: the value of an expression is not evaluated until it is needed. Eager evaluation[^]: the value of an expression is evaluated as soon as it gets bound to a variable.

      var arr = new[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };

      var elementsGreaterThan5Lazy = from n in arr
      where n > 5
      select n; // the query doesn't execute here. If you have a million elements in the array you're not filtering them at this point of the program.

      var elementsGreaterThan5Eager = (from n in arr
      where n > 5
      select n).ToArray(); // the query does execute here because you're forcing the execution by creating an array of the results (you want those values > 5 now in an array).

      The first expression actually evaluates to an iterator (an object that is to perform the desired operation) not the actual result of the operation (the elements which are greater than 5). In the second expression, the ToArray method is forcing the execution of the query to get the elements as an array. In other words, you want an actual array now so the query has to be executed to get the array. Let's say you then write something like

      var firstElementGreaterThan5 = elementsGreaterThan5Lazy.First();

      Here you're forcing the query to execute to get the first element that is greater than 5 (and to throw an exception when the sequence is empty). Actually, you're not looping over all the elements, you're just looping until you find the desired value. In other words, if you have a million elements in the array and the first element greater than 5 is in the second location, you're actually doing 2 iterations and not iterating over the whole million elements. Some extension methods like ToArray, ToList, First, Last... do force the execution of the query. While other methods like Where, Take, Skip... do not execute the query but return an iterator. You can tell whether a method forces the execution or not from its name or from the documentation. I hope that answers your question.

      Eslam Afifi

      M 1 Reply Last reply
      0
      • E Eslam Afifi

        Lazy evaluation[^]: the value of an expression is not evaluated until it is needed. Eager evaluation[^]: the value of an expression is evaluated as soon as it gets bound to a variable.

        var arr = new[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };

        var elementsGreaterThan5Lazy = from n in arr
        where n > 5
        select n; // the query doesn't execute here. If you have a million elements in the array you're not filtering them at this point of the program.

        var elementsGreaterThan5Eager = (from n in arr
        where n > 5
        select n).ToArray(); // the query does execute here because you're forcing the execution by creating an array of the results (you want those values > 5 now in an array).

        The first expression actually evaluates to an iterator (an object that is to perform the desired operation) not the actual result of the operation (the elements which are greater than 5). In the second expression, the ToArray method is forcing the execution of the query to get the elements as an array. In other words, you want an actual array now so the query has to be executed to get the array. Let's say you then write something like

        var firstElementGreaterThan5 = elementsGreaterThan5Lazy.First();

        Here you're forcing the query to execute to get the first element that is greater than 5 (and to throw an exception when the sequence is empty). Actually, you're not looping over all the elements, you're just looping until you find the desired value. In other words, if you have a million elements in the array and the first element greater than 5 is in the second location, you're actually doing 2 iterations and not iterating over the whole million elements. Some extension methods like ToArray, ToList, First, Last... do force the execution of the query. While other methods like Where, Take, Skip... do not execute the query but return an iterator. You can tell whether a method forces the execution or not from its name or from the documentation. I hope that answers your question.

        Eslam Afifi

        M Offline
        M Offline
        mbabube
        wrote on last edited by
        #3

        Dear Eslam Afifi, Thanks a lot for your explanation.You have explained the difference between lazy and eager evalation.I could find lot of explanation on the same topic in text books and in the internet.However my question is how we can implement eager evalation for deferred execution or how you example for the exger evaluation employs Deferred execution.Could you please provied any example for that? Moreover,how deferred execution differs from lazy evaluation?In my point of view,both are looking same.Could you please provide any example for this too? Thanks in Advance...!

        E 1 Reply Last reply
        0
        • M mbabube

          Dear Eslam Afifi, Thanks a lot for your explanation.You have explained the difference between lazy and eager evalation.I could find lot of explanation on the same topic in text books and in the internet.However my question is how we can implement eager evalation for deferred execution or how you example for the exger evaluation employs Deferred execution.Could you please provied any example for that? Moreover,how deferred execution differs from lazy evaluation?In my point of view,both are looking same.Could you please provide any example for this too? Thanks in Advance...!

          E Offline
          E Offline
          Eslam Afifi
          wrote on last edited by
          #4

          mbabube wrote:

          how we can implement eager evalation for deferred execution or how you example for the exger evaluation employs Deferred execution

          Eslam Afifi wrote:

          var elementsGreaterThan5Eager = (from n in arr where n > 5
          select n).ToArray();

          The query is deferred, using the ToArray method forces the execution of the query making it eager evaluation (at this point of the program you're looping over all the elements selecting those which match the criteria and putting them in an array). The eager evaluation of ToArray employs (uses) the deferred execution of the query. About the difference between deferred execution and lazy evaluation, the way I see it is that lazy evaluation gives you all the result value when you need it while deferred execution is executing only the part you need from an expression/query (not necessarily all of it) to evaluate another expression/query. I hope I understood your question right and that I gave you a good answer. And sorry for the late reply.

          Eslam Afifi

          M 1 Reply Last reply
          0
          • E Eslam Afifi

            mbabube wrote:

            how we can implement eager evalation for deferred execution or how you example for the exger evaluation employs Deferred execution

            Eslam Afifi wrote:

            var elementsGreaterThan5Eager = (from n in arr where n > 5
            select n).ToArray();

            The query is deferred, using the ToArray method forces the execution of the query making it eager evaluation (at this point of the program you're looping over all the elements selecting those which match the criteria and putting them in an array). The eager evaluation of ToArray employs (uses) the deferred execution of the query. About the difference between deferred execution and lazy evaluation, the way I see it is that lazy evaluation gives you all the result value when you need it while deferred execution is executing only the part you need from an expression/query (not necessarily all of it) to evaluate another expression/query. I hope I understood your question right and that I gave you a good answer. And sorry for the late reply.

            Eslam Afifi

            M Offline
            M Offline
            mbabube
            wrote on last edited by
            #5

            Dear Eslam Afifi, Thanks a lot for your detailed answer.

            E 1 Reply Last reply
            0
            • M mbabube

              Dear Eslam Afifi, Thanks a lot for your detailed answer.

              E Offline
              E Offline
              Eslam Afifi
              wrote on last edited by
              #6

              You're welcome.

              Eslam Afifi

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups