<?xml version="1.0" encoding="utf-8" ?>
<?xml-stylesheet type="text/xsl" href="RSS_xslt_style.asp" version="1.0" ?>
<rss version="2.0" xmlns:WebWizForums="http://syndication.webwiz.co.uk/rss_namespace/">
 <channel>
  <title>DevForce Community Forum : Include() combined with FirstOrDefault()</title>
  <link>http://www.ideablade.com/forum/</link>
  <description>This is an XML content feed of; DevForce Community Forum : DevForce 2009 : Include() combined with FirstOrDefault()</description>
  <pubDate>Tue, 28 Apr 2026 05:00:54 -700</pubDate>
  <lastBuildDate>Mon, 16 Mar 2009 08:47:04 -700</lastBuildDate>
  <docs>http://blogs.law.harvard.edu/tech/rss</docs>
  <generator>Web Wiz Forums 9.69</generator>
  <ttl>360</ttl>
  <WebWizForums:feedURL>www.ideablade.com/forum/RSS_post_feed.asp?TID=1139</WebWizForums:feedURL>
  <image>
   <title>DevForce Community Forum</title>
   <url>http://www.ideablade.com/forum/forum_images/IdeaBlade_logo_tm.png</url>
   <link>http://www.ideablade.com/forum/</link>
  </image>
  <item>
   <title>Include() combined with FirstOrDefault() : Ok thanks for the info. </title>
   <link>http://www.ideablade.com/forum/forum_posts.asp?TID=1139&amp;PID=4085#4085</link>
   <description>
    <![CDATA[<strong>Author:</strong> <a href="http://www.ideablade.com/forum/member_profile.asp?PF=452" rel="nofollow">ken.nelson</a><br /><strong>Subject:</strong> 1139<br /><strong>Posted:</strong> 16-Mar-2009 at 8:47am<br /><br />Ok thanks for the info.]]>
   </description>
   <pubDate>Mon, 16 Mar 2009 08:47:04 -700</pubDate>
   <guid isPermaLink="true">http://www.ideablade.com/forum/forum_posts.asp?TID=1139&amp;PID=4085#4085</guid>
  </item> 
  <item>
   <title>Include() combined with FirstOrDefault() : This is actually a known bug which...</title>
   <link>http://www.ideablade.com/forum/forum_posts.asp?TID=1139&amp;PID=4074#4074</link>
   <description>
    <![CDATA[<strong>Author:</strong> <a href="http://www.ideablade.com/forum/member_profile.asp?PF=11" rel="nofollow">kimj</a><br /><strong>Subject:</strong> 1139<br /><strong>Posted:</strong> 13-Mar-2009 at 4:46pm<br /><br />This is actually a known bug which we'd hoped to fix in the 4.3.0 release but did not.&nbsp; You can use the&nbsp;.Execute() or .ToList() methods as a workaround for now, but more data will be returned than wanted.&nbsp; Using either of these will submit the full query to EF and the database and then invoke FirstOrDefault() on the results returned to the EM cache.&nbsp;]]>
   </description>
   <pubDate>Fri, 13 Mar 2009 16:46:29 -700</pubDate>
   <guid isPermaLink="true">http://www.ideablade.com/forum/forum_posts.asp?TID=1139&amp;PID=4074#4074</guid>
  </item> 
  <item>
   <title>Include() combined with FirstOrDefault() : Hi, Have you tried doing this?em.Customer.Where(i...</title>
   <link>http://www.ideablade.com/forum/forum_posts.asp?TID=1139&amp;PID=4071#4071</link>
   <description>
    <![CDATA[<strong>Author:</strong> <a href="http://www.ideablade.com/forum/member_profile.asp?PF=449" rel="nofollow">smi-mark</a><br /><strong>Subject:</strong> 1139<br /><strong>Posted:</strong> 13-Mar-2009 at 11:47am<br /><br />Hi, <br><br>Have you tried doing this?<br><br><font face="Courier New, Courier, mono">em.Customer.Where(i =&gt; i.Name.BeginsWith("R")).Include("Orders").ToList().FirstOrDefault();</font><br>]]>
   </description>
   <pubDate>Fri, 13 Mar 2009 11:47:14 -700</pubDate>
   <guid isPermaLink="true">http://www.ideablade.com/forum/forum_posts.asp?TID=1139&amp;PID=4071#4071</guid>
  </item> 
  <item>
   <title>Include() combined with FirstOrDefault() : Hello, In Microsoft EF, we were...</title>
   <link>http://www.ideablade.com/forum/forum_posts.asp?TID=1139&amp;PID=4063#4063</link>
   <description>
    <![CDATA[<strong>Author:</strong> <a href="http://www.ideablade.com/forum/member_profile.asp?PF=452" rel="nofollow">ken.nelson</a><br /><strong>Subject:</strong> 1139<br /><strong>Posted:</strong> 13-Mar-2009 at 8:02am<br /><br /><P>Hello,</P><P>In Microsoft EF, we were able to do something like the following:</P><DIV><FONT face="Courier New, Courier, mono">// In Microsoft EF, c.Orders.Count == 7</FONT></DIV><DIV><FONT face="Courier New, Courier, mono">Customer c = em.Customer.Where(i =&gt; i.Name.BeginsWith("R")).Include("Orders").FirstOrDefault();</FONT></DIV><DIV><FONT face="Courier New, Courier, mono">if (c != null)</FONT></DIV><DIV><FONT face="Courier New, Courier, mono">&nbsp;&nbsp;&nbsp;&nbsp; Console.WriteLine(c.Orders.Count);</FONT></DIV><DIV>&nbsp;</DIV><DIV>And as expected, the first&nbsp;Customer or null would be returned, and if not null the print out of the number of orders would be accurate.&nbsp; When we try the same in DevForce EF it doesn't appear to work the same.&nbsp; (I need to note that we're talking straight DevForce EF here, not SL).&nbsp; The Customer will come back, but the number of orders is always 0, as if the Include() is not being used.</DIV><DIV>&nbsp;</DIV><DIV><FONT face="Courier New">// In DevForceEF, c.Orders.Count == 0</FONT></DIV><DIV><FONT face="Courier New, Courier, mono">Customer c = em.Customer.Where(i =&gt; i.Name.BeginsWith("R")).Include("Orders").FirstOrDefault();</FONT></DIV><DIV><DIV><FONT face="Courier New, Courier, mono">if (c != null)</FONT></DIV><DIV><FONT face="Courier New, Courier, mono">&nbsp;&nbsp;&nbsp;&nbsp; Console.WriteLine(c.Orders.Count);</FONT></DIV><DIV><FONT face="Courier New"></FONT>&nbsp;</DIV></DIV><DIV>What does seem to work is either of the following:</DIV><DIV>&nbsp;</DIV><DIV><FONT face="Courier New, Courier, mono">Customer c = null;</FONT></DIV><DIV><FONT face="Courier New, Courier, mono">foreach (var item in em.Customer.Where(i =&gt; i.Name.BeginsWith("R")).Include("Orders"))</FONT></DIV><DIV><FONT face="Courier New">{</FONT></DIV><DIV><FONT face="Courier New, Courier, mono">&nbsp;&nbsp;&nbsp;&nbsp; c = item;</FONT></DIV><DIV><FONT face="Courier New">&nbsp;&nbsp;&nbsp;&nbsp; break;</FONT></DIV><DIV><FONT face="Courier New">}</FONT></DIV><DIV><DIV><FONT face="Courier New, Courier, mono">if (c != null)</FONT></DIV><DIV><FONT face="Courier New, Courier, mono">&nbsp;&nbsp;&nbsp;&nbsp; Console.WriteLine(c.Orders.Count);</FONT></DIV><DIV>&nbsp;</DIV><DIV>Or:</DIV><DIV>&nbsp;</DIV><DIV><DIV><FONT face="Courier New, Courier, mono">Customer c = em.Customer.Where(i =&gt; i.Name.BeginsWith("R")).Include("Orders").<strong><FONT color=#cc0000>Execute()</FONT></strong>.FirstOrDefault();</FONT></DIV><DIV><FONT face="Courier New, Courier, mono">if (c != null)</FONT></DIV><DIV><FONT face="Courier New, Courier, mono">&nbsp;&nbsp;&nbsp;&nbsp; Console.WriteLine(c.Orders.Count);</FONT></DIV><DIV>&nbsp;</DIV><DIV>However,&nbsp;if we change the FirstOrDefault() to FirstOrNullEntity(), the "Execute()" solution will fail to compile.</DIV><DIV>&nbsp;</DIV><DIV>Is this working as expected or are we doing something wrong?</DIV><DIV>&nbsp;</DIV><DIV>Thanks,</DIV><DIV>Ken</DIV></DIV></DIV><DIV>&nbsp;</DIV><span style="font-size:10px"><br /><br />Edited by ken.nelson - 13-Mar-2009 at 11:15am</span>]]>
   </description>
   <pubDate>Fri, 13 Mar 2009 08:02:29 -700</pubDate>
   <guid isPermaLink="true">http://www.ideablade.com/forum/forum_posts.asp?TID=1139&amp;PID=4063#4063</guid>
  </item> 
 </channel>
</rss>