Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# linq include before-after where

Tags:

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

like image 339
user3857731 Avatar asked Feb 28 '16 16:02

user3857731


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr. Stroustroupe.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.

Is C programming hard?

C is more difficult to learn than JavaScript, but it's a valuable skill to have because most programming languages are actually implemented in C. This is because C is a “machine-level” language. So learning it will teach you how a computer works and will actually make learning new languages in the future easier.


1 Answers

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.

like image 83
Rex M Avatar answered Sep 22 '22 17:09

Rex M