4
\$\begingroup\$

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();
\$\endgroup\$

1 Answer 1

6
\$\begingroup\$

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.

\$\endgroup\$
1
  • 1
    \$\begingroup\$ I agree, I think it is better to use consistent syntax. I prefer expression syntax for joins too. \$\endgroup\$
    – jaffa
    Commented Jul 26, 2011 at 16:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.