Christopher Bennage

Sponsors

The Lounge

Wicked Cool Jobs

Syndication

It’s Easy to Forget

I’m a big fan of Linq. The more I learn about functional programming, the more I love (and use) Linq.

However with any higher level abstraction there is the possibility of the mysterious inner workings differing from your intuitive expectations. (This one of the dangers of frameworks being ‘too helpful’ to developers.)

Today I was looking at some code for creating new accounts for a web app. We query to see if the account name and email address are unique before adding a new account.

var matching = from account in _session.Linq<Account>()
               where account.Name == requeseted.AccountName
                     || account.EmailAddress == requeseted.EmailAddress
               select account;

I had a single query using NHibernate.Linq. Then I used the query in something like this:

if (matching.Count() <= 0) return; // no matches so move on…

if (matching.Any(x => x.Name == requested.AccountName))
{
    result.AddError(x => x.AccountName,
                     "The account name you chose is not available.");
}

if (matching.Any(x => x.EmailAddress == requested.EmailAddress))
{
    result.AddError(x => x.EmailAddress,
                     "That email address is already in use.");
}

When monitoring this with NHProf I found that it executed three separate queries. One for the Count() and one for each Any(). I had only expected one.

Of course, after seeing it I immediately realized why it was happening but I had been ignorantly skipping along when I wrote the code.

It’s an easy mistake to make.


Posted 12-31-2009 2:32 PM by Christopher Bennage
Filed under: ,

[Advertisement]

Comments

Amith wrote re: It’s Easy to Forget
on 12-31-2009 5:12 PM

From what i understand, the sql query is fired only when we enumerate over it or do any aggregate operation. so, after the first count call, shouldnt all the data have been loaded into the list? as in, the next two queries should have simply read from the memory, right?

What is the right way to write the above code?

Christopher Bennage wrote re: It’s Easy to Forget
on 12-31-2009 5:36 PM

That was the behavior I had in mind when I wrote the code, but it is not how it worked.  It's best to think of 'matches' as a query and not as the results of the query. Which that in mind, the call to matches.Count() is really extending the original query and then executing this new modified query. The original query isn't never executed.

I'm assuming that it's up to the linq provider to determine this sort of behavior.

The simplest solution was for me to call ToList() on the query and then use the resulting in-memory list for the three checks.

James wrote re: It’s Easy to Forget
on 12-31-2009 6:21 PM

Amith, how would LINQ know that's what you intend? Because it may not be (e.g. memory constraints).

Chris is right, think of it as a query. If you find yourself iterating over it more than once, "snapshot" it with ToArray() or ToList() before using.

Amith wrote re: It’s Easy to Forget
on 01-01-2010 8:58 AM

yeah, that makes sense. Using ToList() to have the query execute itself first. thanks!

Chris Eargle wrote re: It’s Easy to Forget
on 01-06-2010 1:43 PM

I prefer ToArray() as it's lighter weight.

It's not possible for Count() to be less than 0 unless it overflows or something. Instead of "matching.Count() <= 0" use "!matching.Any()." see my blog entry on Any vs. Count: www.kodefuguru.com/.../Any-versus-Count.aspx

About The CodeBetter.Com Blog Network
CodeBetter.Com FAQ

Our Mission

Advertisers should contact Brendan

Subscribe
Google Reader or Homepage

del.icio.us CodeBetter.com Latest Items
Add to My Yahoo!
Subscribe with Bloglines
Subscribe in NewsGator Online
Subscribe with myFeedster
Add to My AOL
Furl CodeBetter.com Latest Items
Subscribe in Rojo

Member Projects
DimeCasts.Net - Derik Whittaker

Friends of Devlicio.us
Red-Gate Tools For SQL and .NET

NDepend

SlickEdit
 
SmartInspect .NET Logging
NGEDIT: ViEmu and Codekana
LiteAccounting.Com
DevExpress
Fixx
NHibernate Profiler
Unfuddle
Balsamiq Mockups
Scrumy
JetBrains - ReSharper
<-- NEW Friend!

 



Site Copyright © 2007 CodeBetter.Com
Content Copyright Individual Bloggers

 

Community Server (Commercial Edition)

CodeBetter.Com