c#linq包括之前

发布于 2025-02-09 06:06:48 字数 816 浏览 3 评论 0原文

在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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

勿忘心安 2025-02-16 06:06:48

像您所显示的那样的指令的订购不会在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 a First() just get pushed over to the WHERE clause. The Include statements also get accumulated and projected to JOINs 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文