C # linq includes before and after where

In linq, there is 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) 

Note that using the first query includes after where and where before, but the result is the same. Also how to see the actual sql query? In this particular case, the performance is my main goal, can I improve the query? I need to change two properties: the UserQuizes property and the UserQuizes-> VerbalQuizes-> Question property.

It would be better to split it into two queries or use it as it is

+5
source share
1 answer

Ordering instructions, as you've often shown, will not matter in EF or LINQ to SQL. The query designer turns your entire LINQ statement into an abstract logical representation, and then another pass transforms the logical structure into an SQL statement. Thus, WHERE predicates will all be in one place. The predicates inside a First() simply jump to the WHERE . Include statements are also accumulated and projected onto the JOIN to include additional columns needed to create the included object.

So the short answer is that EF usually produces the most logical SQL statement, regardless of the order in which you created the LINQ statement. If you need to configure it further, you should look at the stored procedure in which you can process SQL.

+5
source

Source: https://habr.com/ru/post/1244054/


All Articles