<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://msmvps.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Joacim's view on stuff : linq</title><link>http://msmvps.com/blogs/joacim/archive/tags/linq/default.aspx</link><description>Tags: linq</description><dc:language>en</dc:language><generator>CommunityServer 2008.5 SP2 (Build: 40407.4157)</generator><item><title>Run you later - Understanding deferred execution in LINQ</title><link>http://msmvps.com/blogs/joacim/archive/2009/09/09/run-you-later-understanding-deferred-execution-in-linq.aspx</link><pubDate>Wed, 09 Sep 2009 13:54:00 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1722055</guid><dc:creator>Joacim Andersson</dc:creator><slash:comments>3</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/joacim/rsscomments.aspx?PostID=1722055</wfw:commentRss><comments>http://msmvps.com/blogs/joacim/archive/2009/09/09/run-you-later-understanding-deferred-execution-in-linq.aspx#comments</comments><description>&lt;p&gt;&lt;strong&gt;Introduction&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;In this article I&amp;rsquo;m going to try to explain one of the most misunderstood features of LINQ &amp;ndash; &lt;em&gt;deferred execution&lt;/em&gt; - and the impact it might have on your code if you don&amp;rsquo;t fully understand what it means.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;What does deferred execution mean?&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;According to the dictionary the word &lt;em&gt;deferred&lt;/em&gt; means &lt;em&gt;delayed&lt;/em&gt; or &lt;em&gt;postponed&lt;/em&gt;. So &lt;em&gt;deferred execution&lt;/em&gt; in LINQ means that the actual execution of a query is postponed to a later time. Let&amp;rsquo;s say that you&amp;rsquo;re using LINQ to SQL and want to use the following LINQ query:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color:blue;"&gt;Dim &lt;/span&gt;cheapProducts = &lt;span style="color:blue;"&gt;From &lt;/span&gt;p &lt;span style="color:blue;"&gt;In &lt;/span&gt;db.Products _
                    &lt;span style="color:blue;"&gt;Where &lt;/span&gt;p.ListPrice &amp;lt; 100 _
                    &lt;span style="color:blue;"&gt;Select &lt;/span&gt;p&lt;/pre&gt;
&lt;p&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;It&amp;rsquo;s easy to assume that the above will execute a query against the underlying database, but it wont. It will just create an IQueryable that has a reference to something that knows how this query can be executed (when it comes to LINQ to SQL this reference will point to an e&lt;em&gt;xpression tree&lt;/em&gt;, but you don&amp;rsquo;t have to know about that since that&amp;rsquo;s just an implementation that the LINQ to SQL provider uses), but it will not run the actual query. &lt;em&gt;LINQ has postponed, or deferred, the execution to a later time&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;-To a later time, you say. So when is that exactly?&lt;/p&gt;
&lt;p&gt;In LINQ the execution of a query is performed when you request the data, for example in a loop.&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color:blue;"&gt;For Each &lt;/span&gt;prod &lt;span style="color:blue;"&gt;In &lt;/span&gt;cheapProducts
  Console.WriteLine(&lt;span style="color:#a31515;"&gt;&amp;quot;The list price for {0} is {1}&amp;quot;&lt;/span&gt;, prod.ProductName, prod.ListPrice)
&lt;span style="color:blue;"&gt;Next&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;But you might request the data earlier. Take the following query as an example.&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color:blue;"&gt;Dim &lt;/span&gt;productList = (&lt;span style="color:blue;"&gt;From &lt;/span&gt;p &lt;span style="color:blue;"&gt;In &lt;/span&gt;db.Products _
                   &lt;span style="color:blue;"&gt;Where &lt;/span&gt;p.ListPrice &amp;lt; 100 _
                   &lt;span style="color:blue;"&gt;Select &lt;/span&gt;p).ToList()&lt;/pre&gt;
&lt;p&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;In this case we request that the result of the query is turned into a List(Of Product). To be able to do that the query has to be executed. But that doesn&amp;rsquo;t really mean that we didn&amp;rsquo;t have a deferred execution, it just wasn&amp;rsquo;t delayed for very long since we immediately request that the data should be turned into a list. This would also be true if we instead of requesting a list would call the Count() extension method or anything else that wouldn&amp;rsquo;t return an IEnumerable(Of T) or an IQueryable(Of T).&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;What kind of implications might this have on my code?&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;For the purpose of this article I&amp;rsquo;m going to use a Person class that only have a FirstName and LastName property and a little helper method that fills a List(Of T) with a number of persons, which will be used as the data source of our queries. In other words I&amp;rsquo;m going to use &lt;em&gt;LINQ to Objects&lt;/em&gt;, but the same rules apply even if you use any other LINQ provider such as &lt;em&gt;LINQ to SQL &lt;/em&gt;or &lt;em&gt;LINQ to XML&lt;/em&gt;.&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color:blue;"&gt;Public Class &lt;/span&gt;Person
  &lt;span style="color:blue;"&gt;Private &lt;/span&gt;_firstName &lt;span style="color:blue;"&gt;As String
  Public Property &lt;/span&gt;FirstName() &lt;span style="color:blue;"&gt;As String
    Get
      Return &lt;/span&gt;_firstName
    &lt;span style="color:blue;"&gt;End Get
    Set&lt;/span&gt;(&lt;span style="color:blue;"&gt;ByVal &lt;/span&gt;value &lt;span style="color:blue;"&gt;As String&lt;/span&gt;)
      _firstName = value
    &lt;span style="color:blue;"&gt;End Set
  End Property

  Private &lt;/span&gt;_lastName &lt;span style="color:blue;"&gt;As String
  Public Property &lt;/span&gt;LastName() &lt;span style="color:blue;"&gt;As String
    Get
      Return &lt;/span&gt;_lastName
    &lt;span style="color:blue;"&gt;End Get
    Set&lt;/span&gt;(&lt;span style="color:blue;"&gt;ByVal &lt;/span&gt;value &lt;span style="color:blue;"&gt;As String&lt;/span&gt;)
      _lastName = value
    &lt;span style="color:blue;"&gt;End Set
  End Property

  Public Sub New&lt;/span&gt;(&lt;span style="color:blue;"&gt;ByVal &lt;/span&gt;firstName &lt;span style="color:blue;"&gt;As String&lt;/span&gt;, &lt;span style="color:blue;"&gt;ByVal l&lt;/span&gt;astName &lt;span style="color:blue;"&gt;As String&lt;/span&gt;)
    _firstName = firstName
    _lastName = lastName
  &lt;span style="color:blue;"&gt;End Sub

  Public Shared Function &lt;/span&gt;GetPersonList() &lt;span style="color:blue;"&gt;As &lt;/span&gt;List(&lt;span style="color:blue;"&gt;Of &lt;/span&gt;Person)
    &lt;span style="color:blue;"&gt;Dim &lt;/span&gt;list &lt;span style="color:blue;"&gt;As New &lt;/span&gt;List(&lt;span style="color:blue;"&gt;Of &lt;/span&gt;Person)
    &lt;span style="color:blue;"&gt;With &lt;/span&gt;list
      .Add(&lt;span style="color:blue;"&gt;New &lt;/span&gt;Person(&lt;span style="color:#a31515;"&gt;&amp;quot;Bart&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515;"&gt;&amp;quot;Simpson&amp;quot;&lt;/span&gt;))
      .Add(&lt;span style="color:blue;"&gt;New &lt;/span&gt;Person(&lt;span style="color:#a31515;"&gt;&amp;quot;Lisa&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515;"&gt;&amp;quot;Simpson&amp;quot;&lt;/span&gt;))
      .Add(&lt;span style="color:blue;"&gt;New &lt;/span&gt;Person(&lt;span style="color:#a31515;"&gt;&amp;quot;Maggie&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515;"&gt;&amp;quot;Simpson&amp;quot;&lt;/span&gt;))
      .Add(&lt;span style="color:blue;"&gt;New &lt;/span&gt;Person(&lt;span style="color:#a31515;"&gt;&amp;quot;Homer&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515;"&gt;&amp;quot;Simpson&amp;quot;&lt;/span&gt;))
      .Add(&lt;span style="color:blue;"&gt;New &lt;/span&gt;Person(&lt;span style="color:#a31515;"&gt;&amp;quot;Marge&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515;"&gt;&amp;quot;Simpson&amp;quot;&lt;/span&gt;))
      .Add(&lt;span style="color:blue;"&gt;New &lt;/span&gt;Person(&lt;span style="color:#a31515;"&gt;&amp;quot;Ned&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515;"&gt;&amp;quot;Flanders&amp;quot;&lt;/span&gt;))
      .Add(&lt;span style="color:blue;"&gt;New &lt;/span&gt;Person(&lt;span style="color:#a31515;"&gt;&amp;quot;Maude&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515;"&gt;&amp;quot;Flanders&amp;quot;&lt;/span&gt;))
      .Add(&lt;span style="color:blue;"&gt;New &lt;/span&gt;Person(&lt;span style="color:#a31515;"&gt;&amp;quot;Rod&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515;"&gt;&amp;quot;Flanders&amp;quot;&lt;/span&gt;))
      .Add(&lt;span style="color:blue;"&gt;New &lt;/span&gt;Person(&lt;span style="color:#a31515;"&gt;&amp;quot;Todd&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515;"&gt;&amp;quot;Flanders&amp;quot;&lt;/span&gt;))
    &lt;span style="color:blue;"&gt;End With
    Return &lt;/span&gt;list
  &lt;span style="color:blue;"&gt;End Function
End Class&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;Now let&amp;rsquo;s say we have the following code.&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color:blue;"&gt;Public Sub &lt;/span&gt;Main()
  &lt;span style="color:blue;"&gt;Dim &lt;/span&gt;personList &lt;span style="color:blue;"&gt;As &lt;/span&gt;List(&lt;span style="color:blue;"&gt;Of &lt;/span&gt;Person) = Person.GetPersonList()

  &lt;span style="color:green;"&gt;&amp;#39;Get a list of the Simpsons family
  &lt;/span&gt;&lt;span style="color:blue;"&gt;Dim &lt;/span&gt;lastName &lt;span style="color:blue;"&gt;As String &lt;/span&gt;= &lt;span style="color:#a31515;"&gt;&amp;quot;Simpson&amp;quot;
  &lt;/span&gt;&lt;span style="color:blue;"&gt;Dim &lt;/span&gt;persons = &lt;span style="color:blue;"&gt;From &lt;/span&gt;p &lt;span style="color:blue;"&gt;In &lt;/span&gt;personList _
                &lt;span style="color:blue;"&gt;Where &lt;/span&gt;p.LastName = lastName _
                &lt;span style="color:blue;"&gt;Select &lt;/span&gt;p

  lastName = &lt;span style="color:#a31515;"&gt;&amp;quot;Flanders&amp;quot;
  &lt;/span&gt;&lt;span style="color:blue;"&gt;For Each &lt;/span&gt;pers &lt;span style="color:blue;"&gt;In &lt;/span&gt;persons
    Console.WriteLine(&lt;span style="color:#a31515;"&gt;&amp;quot;{0} {1}&amp;quot;&lt;/span&gt;, pers.FirstName, pers.LastName)
  &lt;span style="color:blue;"&gt;Next
&lt;/span&gt;&lt;span style="color:blue;"&gt;End Sub&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Which are the names that will be written to the console? If you guessed the names of the Simpsons, you haven&amp;rsquo;t paid attention. The query asks for all persons with the last name &amp;ldquo;Simpson&amp;rdquo;, but still the name of the Flanders are written out. Remember that the query is &lt;strong&gt;not&lt;/strong&gt; executed when we define it but rather when we ask for the data. Since we change the &lt;em&gt;lastName&lt;/em&gt; variable before the query is executed we have in fact changed the query.&lt;/p&gt;
&lt;p&gt;The fact that the execution is delayed until we ask for the data also means that we can &lt;em&gt;reuse&lt;/em&gt; the same query. Let&amp;rsquo;s make some slight changes to the last example.&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color:blue;"&gt;Public Sub &lt;/span&gt;Main()
  &lt;span style="color:blue;"&gt;Dim &lt;/span&gt;personList &lt;span style="color:blue;"&gt;As &lt;/span&gt;List(&lt;span style="color:blue;"&gt;Of &lt;/span&gt;Person) = Person.GetPersonList()

  &lt;span style="color:green;"&gt;&amp;#39;Get a list of the Simpson&amp;#39;s family
  &lt;/span&gt;&lt;span style="color:blue;"&gt;Dim &lt;/span&gt;lastName &lt;span style="color:blue;"&gt;As String &lt;/span&gt;= &lt;span style="color:#a31515;"&gt;&amp;quot;Simpson&amp;quot;
  &lt;/span&gt;&lt;span style="color:blue;"&gt;Dim &lt;/span&gt;persons = &lt;span style="color:blue;"&gt;From &lt;/span&gt;p &lt;span style="color:blue;"&gt;As &lt;/span&gt;Person &lt;span style="color:blue;"&gt;In &lt;/span&gt;personList _
                &lt;span style="color:blue;"&gt;Where &lt;/span&gt;p.LastName = lastName _
                &lt;span style="color:blue;"&gt;Select &lt;/span&gt;p

  &lt;span style="color:blue;"&gt;For Each &lt;/span&gt;pers &lt;span style="color:blue;"&gt;In &lt;/span&gt;persons
    Console.WriteLine(&lt;span style="color:#a31515;"&gt;&amp;quot;{0} {1}&amp;quot;&lt;/span&gt;, pers.FirstName, pers.LastName)
  &lt;span style="color:blue;"&gt;Next
  &lt;/span&gt;Console.WriteLine(&lt;span style="color:#a31515;"&gt;&amp;quot;---------------&amp;quot;&lt;/span&gt;)
  &lt;span style="color:green;"&gt;&amp;#39;Change the last name
  &lt;/span&gt;lastName = &lt;span style="color:#a31515;"&gt;&amp;quot;Flanders&amp;quot;
  &lt;/span&gt;&lt;span style="color:green;"&gt;&amp;#39;Reuse the query
  &lt;/span&gt;&lt;span style="color:blue;"&gt;For Each &lt;/span&gt;pers &lt;span style="color:blue;"&gt;In &lt;/span&gt;persons
    Console.WriteLine(&lt;span style="color:#a31515;"&gt;&amp;quot;{0} {1}&amp;quot;&lt;/span&gt;, pers.FirstName, pers.LastName)
  &lt;span style="color:blue;"&gt;Next
  &lt;/span&gt;Console.ReadLine()
&lt;span style="color:blue;"&gt;End Sub&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;In this example we first write out the names of the Simpson family. Then we change the &lt;em&gt;lastName&lt;/em&gt; variable and loop through it again. Since the query has been changed it will be executed a second time and we&amp;rsquo;ll get the names of the Flanders family. We &lt;em&gt;reused&lt;/em&gt; the same query to get two different results.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Conclusion&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;LINQ is wonderful thing. It allows you to do complex things with very few lines of code and that can surely bring a big smile to your face. However, that smile can easily be turned into a stupid looking grin if you don&amp;rsquo;t understand deferred execution. If you don&amp;rsquo;t understand exactly when the query will be executed you might present completely erroneous data to your end users.&lt;/p&gt;
&lt;p&gt;Have fun!&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1722055" width="1" height="1"&gt;</description><category domain="http://msmvps.com/blogs/joacim/archive/tags/vb/default.aspx">vb</category><category domain="http://msmvps.com/blogs/joacim/archive/tags/visual+basic/default.aspx">visual basic</category><category domain="http://msmvps.com/blogs/joacim/archive/tags/delayed/default.aspx">delayed</category><category domain="http://msmvps.com/blogs/joacim/archive/tags/execution/default.aspx">execution</category><category domain="http://msmvps.com/blogs/joacim/archive/tags/deferred+execution/default.aspx">deferred execution</category><category domain="http://msmvps.com/blogs/joacim/archive/tags/postponed/default.aspx">postponed</category><category domain="http://msmvps.com/blogs/joacim/archive/tags/deferred/default.aspx">deferred</category><category domain="http://msmvps.com/blogs/joacim/archive/tags/linq/default.aspx">linq</category></item></channel></rss>