c#linq包括之前
在linq中,有以下区别:
EFDbContext _db = new EFDbContext();
1)_db.UserQuizes
.Where(uq => uq.UserId == currentUserId && uq.QuizId == quizId)
.Include(qz => qz.Quiz.VerbalQuizes.Select(q => q.Question)).First()
2)_db.UserQuizes
.Include(qz => qz.Quiz.VerbalQuizes.Select(q => q.Question))
.Where(uq => uq.UserId == currentUserId && uq.QuizId == quizId).First()
3)_db.UserQuizes
.Include(qz => qz.Quiz.VerbalQuizes.Select(q => q.Question))
First(uq => uq.UserId == currentUserId && uq.QuizId == quizId)
注意,第一个查询使用包括之后和第二个位置,但结果是相同的。还有如何查看实际的SQL查询?在这种特殊情况下,我的主要目标是我可以改进查询吗?我需要更改两个属性:用户对属性和用户的信息 - >言语 - >问题属性。
将其分配两个查询或像它一样使用它会更好
In linq is there a difference between:
EFDbContext _db = new EFDbContext();
1)_db.UserQuizes
.Where(uq => uq.UserId == currentUserId && uq.QuizId == quizId)
.Include(qz => qz.Quiz.VerbalQuizes.Select(q => q.Question)).First()
2)_db.UserQuizes
.Include(qz => qz.Quiz.VerbalQuizes.Select(q => q.Question))
.Where(uq => uq.UserId == currentUserId && uq.QuizId == quizId).First()
3)_db.UserQuizes
.Include(qz => qz.Quiz.VerbalQuizes.Select(q => q.Question))
First(uq => uq.UserId == currentUserId && uq.QuizId == quizId)
Notice that first query uses include after where and second before where, but result is the same. Also how to see actual sql query? In this particular case perfomance is my main goal, can i improve the query? i need to change two properties : UserQuizes property, and UserQuizes-> VerbalQuizes-> Question property.
Would it be better to split up it two queries or use it like as it is
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
像您所显示的那样的指令的订购不会在EF或LINQ到SQL上有所作为。查询构建器将您的整个LINQ语句变成一个抽象的逻辑表示形式,然后另一个通过将逻辑结构转换为SQL语句。因此,谓词都将最终到达同一位置。
first()
中的谓词只需将其推到子句中的。
include
语句还会累积并预测到join
s,以包括产生随附的实体所需的额外列。因此,简短的答案是,无论您构建LINQ语句的顺序如何,EF通常会产生最逻辑的SQL语句。如果需要进一步调整,则应查看可以将SQL手工处理的存储过程。
Ordering of instructions like you've shown often won't make a difference in EF or LINQ to SQL. The query builder turns your entire LINQ statement into an abstract logical representation, and then another pass converts the logical structure into a SQL statement. So the
WHERE
predicates are all going to end up in the same place. The predicates inside aFirst()
just get pushed over to theWHERE
clause. TheInclude
statements also get accumulated and projected toJOIN
s to include the extra columns needed to produce the included entity.So the short answer is that EF will usually produce the most logical SQL statement regardless of the order in which you constructed your LINQ statement. If you need to tune it further, you should look at a stored procedure where you can hand-craft the SQL.