<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/'><id>tag:blogger.com,1999:blog-13073408.post8420945807895053814..comments</id><updated>2011-08-05T07:18:43.144-04:00</updated><category term='c#'/><category term='LINQ'/><category term='Visual Studio'/><category term='technology'/><category term='csharp'/><category term='MVC'/><category term='bugs'/><category term='development'/><category term='programming'/><category term='Razor'/><category term='web development'/><category term='productivity'/><category term='Windows7'/><category term='Windows'/><category term='IIS'/><category term='networking'/><category term='hardware'/><category term='database'/><category term='.NET'/><category term='ASP.NET'/><category term='T4MVC'/><title type='text'>Comments on Compiled Thoughts: Adventures in LINQ: Deferred execution</title><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://www.compiledthoughts.com/feeds/8420945807895053814/comments/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/13073408/8420945807895053814/comments/default'/><link rel='alternate' type='text/html' href='http://www.compiledthoughts.com/2009/01/adventures-in-linq-deferred-execution.html'/><author><name>Peter Lanoie</name><uri>http://www.blogger.com/profile/11480942345027154915</uri><email>noreply@blogger.com</email><gd:image xmlns:gd='http://schemas.google.com/g/2005' rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='http://2.bp.blogspot.com/_1ODj8s-E01o/SpfoAWkvESI/AAAAAAAAAK8/6z4OkQUZQEk/s1600-R/1f8b331ee1b89573b7632226b8e6632b.png'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>2</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>25</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-13073408.post-5905639924747881290</id><published>2009-02-01T10:32:00.000-05:00</published><updated>2009-02-01T10:32:00.000-05:00</updated><title type='text'>Imar, &lt;br&gt;&lt;br&gt;Thanks as always for the insight.  A...</title><content type='html'>Imar, &lt;BR/&gt;&lt;BR/&gt;Thanks as always for the insight.  As I'm still quite new to LINQ, I hadn't yet explored the ToList() method.  I'll give that a try.  I have used the SingleOrDefault() method. Very handy. In this example, I only need to check that the file exists, no need to get any more details.  I'll make some changes and profile it.</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/13073408/8420945807895053814/comments/default/5905639924747881290'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/13073408/8420945807895053814/comments/default/5905639924747881290'/><link rel='alternate' type='text/html' href='http://www.compiledthoughts.com/2009/01/adventures-in-linq-deferred-execution.html?showComment=1233502320000#c5905639924747881290' title=''/><author><name>Peter Lanoie</name><uri>http://www.blogger.com/profile/11480942345027154915</uri><email>noreply@blogger.com</email><gd:image xmlns:gd='http://schemas.google.com/g/2005' rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='http://1.bp.blogspot.com/_1ODj8s-E01o/SQRQjmbckQI/AAAAAAAAAEw/BNHsEr2_eJ0/S220/WebIcon.jpg'/></author><thr:in-reply-to xmlns:thr='http://purl.org/syndication/thread/1.0' href='http://www.compiledthoughts.com/2009/01/adventures-in-linq-deferred-execution.html' ref='tag:blogger.com,1999:blog-13073408.post-8420945807895053814' source='http://www.blogger.com/feeds/13073408/posts/default/8420945807895053814' type='text/html'/><gd:extendedProperty xmlns:gd='http://schemas.google.com/g/2005' name='blogger.itemClass' value='pid-1229900851'/></entry><entry><id>tag:blogger.com,1999:blog-13073408.post-4639689741396271785</id><published>2009-02-01T04:36:00.000-05:00</published><updated>2009-02-01T04:36:00.000-05:00</updated><title type='text'>Hi Peter,&lt;br&gt;&lt;br&gt;The deferred execution model is b...</title><content type='html'>Hi Peter,&lt;BR/&gt;&lt;BR/&gt;The deferred execution model is by design; usually it&amp;#39;s good as it allows you to query other queries before executing them, enabling you to limit your final data set before hitting the database.&lt;BR/&gt;&lt;BR/&gt;Your GetEnumerator solution is close; however, instead of that, you should call ToList and store the results in a variable. The following code ensues only a SELECT * query is executed once before the loop while counting is done locally in the loop:&lt;BR/&gt;&lt;BR/&gt;int intPhotoCount;&lt;BR/&gt;string[] strFiles;&lt;BR/&gt;string dirPath = @&amp;quot;C:\SomePath&amp;quot;;&lt;BR/&gt;strFiles = Directory.GetFiles(dirPath, &amp;quot;*.*&amp;quot;);&lt;BR/&gt;var lstPhotos = (from p in _db.Photos select p).ToList();&lt;BR/&gt;for (int i = 0; i &amp;lt; strFiles.Length; i++)&lt;BR/&gt;{&lt;BR/&gt;  intPhotoCount = (from p in lstPhotos&lt;BR/&gt;                       where p.FullPath.ToLower() == strFiles[i].ToLower()&lt;BR/&gt;                       select p).Count();&lt;BR/&gt;  if (intPhotoCount == 0)&lt;BR/&gt;  {&lt;BR/&gt;    // do create photo stuff&lt;BR/&gt;  }&lt;BR/&gt;}&lt;BR/&gt;&lt;BR/&gt;Rather than getting the Count, you can also retrieve the actual Photo (or null) like this:&lt;BR/&gt;&lt;BR/&gt;Photo myPhoto = (from p in lstPhotos&lt;BR/&gt;                         where p.FullPath.ToLower() == strFiles[i].ToLower()&lt;BR/&gt;                         select p).SingleOrDefault();&lt;BR/&gt;&lt;BR/&gt;if (myPhoto != null)&lt;BR/&gt;{&lt;BR/&gt;  // do create photo stuff&lt;BR/&gt;}&lt;BR/&gt;&lt;BR/&gt;&lt;BR/&gt;Finally, you could do some funny JOIN stuff like this:&lt;BR/&gt;&lt;BR/&gt;string[] strFiles;&lt;BR/&gt;string dirPath = @&amp;quot;C:\SomePath&amp;quot;;&lt;BR/&gt;strFiles = Directory.GetFiles(dirPath, &amp;quot;*.*&amp;quot;);&lt;BR/&gt;var orphanedFiles = (from o in strFiles&lt;BR/&gt;                             join a in _db.Photos on o equals a.FullPath into myFiles&lt;BR/&gt;                             where !_db.Photos.Any(p =&amp;gt; p.FullPath == o)&lt;BR/&gt;                              select o).ToList();&lt;BR/&gt;&lt;BR/&gt;foreach (var orphanedFile in orphanedFiles)&lt;BR/&gt;{&lt;BR/&gt;  // Work with file here&lt;BR/&gt;}&lt;BR/&gt;&lt;BR/&gt;&lt;BR/&gt;Your Profiler will go crazy and you&amp;#39;ll see a lot of queries being executed; but it&amp;#39;s cool that it works nonetheless....&lt;BR/&gt;&lt;BR/&gt;Code formatting is not ideal here, but I am sure yo get the idea....&lt;BR/&gt;&lt;BR/&gt;Cheers,&lt;BR/&gt;&lt;BR/&gt;Imar</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/13073408/8420945807895053814/comments/default/4639689741396271785'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/13073408/8420945807895053814/comments/default/4639689741396271785'/><link rel='alternate' type='text/html' href='http://www.compiledthoughts.com/2009/01/adventures-in-linq-deferred-execution.html?showComment=1233480960000#c4639689741396271785' title=''/><author><name>Imar Spaanjaars</name><uri>http://imar.spaanjaars.com</uri><email>noreply@blogger.com</email><gd:image xmlns:gd='http://schemas.google.com/g/2005' rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img1.blogblog.com/img/blank.gif'/></author><thr:in-reply-to xmlns:thr='http://purl.org/syndication/thread/1.0' href='http://www.compiledthoughts.com/2009/01/adventures-in-linq-deferred-execution.html' ref='tag:blogger.com,1999:blog-13073408.post-8420945807895053814' source='http://www.blogger.com/feeds/13073408/posts/default/8420945807895053814' type='text/html'/><gd:extendedProperty xmlns:gd='http://schemas.google.com/g/2005' name='blogger.itemClass' value='pid-1912011140'/></entry></feed>
