NullHypothesis
NullHypothesis

Reputation: 4506

What is the difference between these two LINQ expressions?

I was doing some messing around with dbcontext / EF and both produce the same thing for my needs. Are these actually equivalent? Any interesting points to consider on the difference?

//Something
var user = dbContext.Set<User>()
.Include(u => u.Preferences).FirstOrDefault(u => u.Id == userID);


//Something else
var user = dbContext.Set<User>().Where(u => u.Id == userID)
.Include(u => u.Preferences).FirstOrDefault();

..Just curious.

Thanks!

Upvotes: 0

Views: 58

Answers (2)

bubi
bubi

Reputation: 6491

Like in @ThomasLevesque answer they're functionally equivalent.

More, EF translate them in the same expression tree (a where expression and a Join/UnionAll - in this case only a Join - expression to solve the Include).
So, a single EF provider returns always the same query.

Upvotes: 0

Thomas Levesque
Thomas Levesque

Reputation: 292355

They're functionally equivalent, and should result in the same SQL query being executed against the database (depending on how the Linq provider is implemented, of course).

Upvotes: 4

Related Questions