dave^2=-1

Now at davesquared.blogspot.com

Sponsors

News

Please visit davesquared.blogspot.com for new content and the most up-to-date versions of all posts.

All code and advice is provided without warranty -- use at your own risk! Opinions expressed here are my own and not that of my employer or any one else. This is just a blog! Don't take it too seriously! Despite not being too serious, this blog has a Privacy Policy, because it uses Google Analytics to see if anyone drops by.

LINQ enumeration gotcha

Rick Strahl has posted about enumerating over LINQ results. Essentially, each item enumerated in a LINQ-to-SQL select fires off a new DB query. This means that modifying the result will not affect the resultset on the next enumeration (which probably sounds obvious, but is easy to code without noticing. Especially if you are working on a result and then databinding as in Rick's example). It also means that repeated traversals will incur a repeated overhead, rather than repeating the work in-memory.

Good to watch out for. Rick suggests using ToList, ToArray, or ToDictionary methods if you want to grab a single, in-memory copy of the result (obviously being mindful of the result size :)).

This was originally posted to my previous blog. You can view the original post and any comments here.

Posted: Aug 17 2007, 09:13 AM by dtchepak | with no comments
Filed under: ,

Comments

No Comments

Leave a Comment

(required) 

(required) 

(optional)

(required)