views:

93

answers:

4

Is there any difference between these two LINQ statements:

var query = from a in entities.A
            where a.Name == "Something"
            from b in entities.B
            where a.Id == b.Id
            select b;


var query = from a in entities.A
            join b in entities.B on a.Id equals b.Id                 
            where a.Name == "Something"
            select b;

Are both statements doing an inner join?

Also how do I view generated the generated SQL statement from the Entity Framework?

+1  A: 

Logically speaking these two statements are doing the same thing. If they are computed differently by the framework then I would be unimpressed.

Kirk Broadhurst
A: 

Take a look to the sql profiler. You could get your answer.

yapiskan
+1  A: 

This doesn't precisely answer your question, but it's nearly always wrong to use join in LINQ to Entities. Both queries are, in my opinion, incorrect. What you actually want to do in this case is:

var query = from a in entities.A
            where a.Name == "Something"
            from b in a.Bs // where Bs is the name of the relationship to B on A, 
            select b;      // whatever it's called

You already have the specification of the relationship encoded in your DB foreign keys and your entity model. Don't duplicate it in your queries.

Craig Stuntz
A: 

You can get, and compare the SQL for, those queries:

((ObjectQuery)query).ToTraceString();

The generated SQL may be (subtly) different depending on how EF interprets those queries.

FYI- You don't have to include joins when querying related entities.

Dave Swersky