[英]Difference between && and where condition in entity framework query
Code 1代码 1
I have using two where condition in my query我在查询中使用了两个 where 条件
dbContext.Projects.Where(p=>p.ProjectId!=ProjectId).Where(p=>p.Name==Name)
.SingleOrDefault();
code 2代码 2
I have using && condition without using two where condition我使用 && 条件而不使用两个 where 条件
dbContext.Projects.Where(p=>p.ProjectId!=ProjectId &&
p.Name==Name).SingleOrDefault();
The both queries are return same value.这两个查询都返回相同的值。 but i don't know the differences.但我不知道有什么区别。 Please explain to me, which one is better.请给我解释一下,哪个更好。 and why?为什么?
If you open your query in LinqPad
you will see that both queries如果您在LinqPad
打开您的查询,您将看到两个查询
dbContext.Projects.Where(p=>p.ProjectId!=ProjectId).Where(p=>p.Name==Name)
dbContext.Projects.Where(p=>p.ProjectId!=ProjectId && p.Name==Name);
will result both in将导致两者
SELECT [...]
FROM [...]
WHERE p.ProjectId <> someId AND p.Name = 'something'
There is no difference neither in performance nor in SQL query structure.无论是性能还是 SQL 查询结构都没有区别。
From the documentation从文档
Return value:返回值:
An IEnumerable that contains elements from the input sequence that satisfy the condition.包含输入序列中满足条件的元素的 IEnumerable。
So the second where
will only be applied to the records surviging the first where
- which is identical to the && expression you built in code 2.因此,第二个where
将仅应用于第一个where
的记录 - 这与您在代码 2 中构建的 && 表达式相同。
See: https://msdn.microsoft.com/de-de/library/bb534803%28v=vs.110%29.aspx请参阅: https : //msdn.microsoft.com/de-de/library/bb534803%28v=vs.110%29.aspx
Both queries are same.两个查询是相同的。 But Query 2
will give you better performance, as &&
will help you in short-circuit .但是Query 2
会给你更好的性能,因为&&
会帮助你解决短路问题。 In the first query, first where
will yield the result and that will be given to 2nd where
clause and then it will evaluate result based on the input.在第一个查询中,首先where
将产生结果,并将其提供给第二个where
子句,然后它将根据输入评估结果。 Let me know if you need more info.如果您需要更多信息,请告诉我。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.