Difference between && and condition in entity frame view

The difference between a condition and a condition and two conditions when the condition is in the query of the entity framework

Code 1

I have two where conditions in my request

dbContext.Projects.Where(p=>p.ProjectId!=ProjectId).Where(p=>p.Name==Name) .SingleOrDefault(); 

code 2

I have an && condition without using two conditions when the condition

  dbContext.Projects.Where(p=>p.ProjectId!=ProjectId && p.Name==Name).SingleOrDefault(); 

  • What is the difference between code1 and code2 ????

Both queries return the same value. but I do not know the differences. Please explain to me which one is better. and why?

+6
source share
3 answers

If you open your query in LinqPad , you will see that both queries

 dbContext.Projects.Where(p=>p.ProjectId!=ProjectId).Where(p=>p.Name==Name) dbContext.Projects.Where(p=>p.ProjectId!=ProjectId && p.Name==Name); 

will lead to

 SELECT [...] FROM [...] WHERE p.ProjectId <> someId AND p.Name = 'something' 

There is no difference in performance or in the structure of SQL queries.

+8
source

From the documentation

Return value:

IEnumerable containing elements from the input sequence that satisfy the condition.

Thus, the second where will only be applied to entries left before the first where , which is identical to the && expression you created in code 2.

See: https://msdn.microsoft.com/de-de/library/bb534803%28v=vs.110%29.aspx

+2
source

Both queries are the same. But Query 2 will give you better performance, as && will help you with the short circuit . In the first query, the first where will give the result, and this will be given to the 2nd where clause, and then will evaluate the result based on the input. Let me know if you need more information.

+1
source

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


All Articles