If it is LINQ-To-Objects, this is important.
Enumerable.Where comparable to an if clause. So this is ...
if(expensiveMethodThatReturnsBool() && num1 < num2) {
... is probably less effective than:
if(num1 < num2 && expensiveMethodThatReturnsBool()) {
because && is a short circuit operator. The second expression is evaluated only on the first return true . The same applies to || , the second is evaluated only if the first returned is false .
7.11 Conditional logical operators
He looks like a chained Where s. Similarly, because predicates apply to every remaining element that has passed through the previous Where .
So this is ...
.Where(x => expensiveMethodThatReturnsBool(x)) .Where(x => x.num1 < x.num2)
may also be less effective than:
.Where(x => x.num1 < x.num2) .Where(x => expensiveMethodThatReturnsBool(x))
The first is logically equivalent:
.Where(x => expensiveMethodThatReturnsBool(x) && x.num1 < x.num2)
source share