简体   繁体   English

这两个LINQ表达式有什么区别?

[英]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. 我在弄乱dbcontext / EF并根据我的需要产生相同的结果。 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. 就像@ThomasLevesque回答一样,它们在功能上是等效的。

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). 此外,EF将它们转换为同一表达式树(其中一个表达式和一个Join / UnionAll-在这种情况下只有一个Join-表达式来解决Include)。
So, a single EF provider returns always the same query. 因此,单个EF提供程序始终返回相同的查询。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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