简体   繁体   中英

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!

它们在功能上是等效的,并且应该导致对数据库执行相同的SQL查询(当然,这取决于Linq提供程序的实现方式)。

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM