HiveBrain v1.2.0
Get Started
← Back to all entries
patterncsharpMinor

Is there any loss in performance converting a 'where' clause to a lambda?

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
lossanywhereperformanceconvertingthereclauselambda

Problem

Is there any different in performance for the following 2 queries.
I'm just wondering what is the better of the two:

var res1 = (from a in _ctx.DataContext.Actions
            join e in _ctx.DataContext.Events on a.EventId equals e.EventId                      
            select a).Single(a => a.ActionId == actionId);


or

var res2 = (from a in _ctx.DataContext.Actions
            join e in _ctx.DataContext.Events on a.EventId equals e.EventId
            where a.ActionId == actionId
            select a).Single();

Solution

There should not be any performance difference that is a result of the syntax used. The query syntax is just eye candy that gets converted to the same underlying code. The difference between the two is really just

_ctx.DataContext.Actions
.Join(_ctx.DataContext.Events, blah, blah, blah)
.Single(a=>a.ActionId == actionId);


vs.

_ctx.DataContext.Actions
.Join(_ctx.DataContext.Events, blah, blah, blah)
.Where(a=>a.ActionId == actionId)
.Single();


If there's a performance difference, I'd be very surprised. The only way to really tell is to run some tests. I personally prefer your second method or my first; as I do not like mixing query syntax with imperative syntax.

Code Snippets

_ctx.DataContext.Actions
.Join(_ctx.DataContext.Events, blah, blah, blah)
.Single(a=>a.ActionId == actionId);
_ctx.DataContext.Actions
.Join(_ctx.DataContext.Events, blah, blah, blah)
.Where(a=>a.ActionId == actionId)
.Single();

Context

StackExchange Code Review Q#3645, answer score: 6

Revisions (0)

No revisions yet.