I am new to LINQ. Could anyone please clarify whether the order of conditions under where clause in a LINQ query matter.
for eg: Is there any difference w.r.t performance between below two queries
Query-1
from prod in Products
where prod.ExpiryDate == Products.Where(s => s.ID.Equals(CurrObj.Id) &&
s.Type.Equals(CurrObj.Type)).Max(s => s.ExpiryDate) &&
prod.ID.Equals(CurrObj.Id) && prod.Type.Equals(CurrObj.Type)
select new
{
AmountFrom = prod.AmountFrom,
AmountTo = prod.AmountTo
}
Query-2
from prod in Products
where prod.ID.Equals(CurrObj.Id) &&
prod.Type.Equals(CurrObj.Type) &&
prod.ExpiryDate == Products.Where(s => s.ID.Equals(CurrObj.Id) && s.Type.Equals(CurrObj.Type)).Max(s => s.ExpiryDate)
select new
{
AmountFrom = prod.AmountFrom,
AmountTo = prod.AmountTo
}
Edit: I just measured the performance. Query-1 takes around 900ms whereas Query-2 takes 350ms.
In LINQ, the OrderBy operator is used to sort the list/ collection values in ascending order. In LINQ, if we use order by the operator by default, it will sort the list of values in ascending order.
Well, you can just put multiple "where" clauses in directly, but I don't think you want to. Multiple "where" clauses ends up with a more restrictive filter - I think you want a less restrictive one.
SelectedValue) . OrderBy(s=> s.Name). ToList(); The order by does nothing, just executes the query, the ToList will do the sort for the original query.
The reason is, LINQ is used with C# or other programming languages, which requires all the variables to be declared first. From clause of LINQ query just defines the range or conditions to select records. So that's why from clause must appear before Select in LINQ.
In SQL Server order does not matter in the WHERE condition. SQL Server does not short circuit conditions as well it does not help in performance. Today is a quick puzzle time. I recently heard from someone it does matter and a senior SQL person was able to reproduce it, but again, I have no proof for it and I have not seen it before.
We have specified two where conditions in both linq and lambda queries. The first where clause checks for the income that is greater than 25,000 and the second where clause looks for the income that is less than 45,000. We can see there is just one income between 25000 and 40000. So we should be getting this as output from both queries.
You need to use either AND or OR clause between conditions of the WHERE clause The performance will be measured using the Actual Execution Plan and SET IO Statistics ON The result set returned from the query should be the same before changing the order of columns in WHERE condition and after changing the order of columns in WHERE condition.
The result set returned from the query should be the same before changing the order of columns in WHERE condition and after changing the order of columns in WHERE condition. Winning solutions will be posted on this blog with due credit.
If it's LINQ-To-Objects it matters.
Enumerable.Where
is comparable to an if
clause. So this ...
if(expensiveMethodThatReturnsBool() && num1 < num2)
{
// ...
}
... is probably less efficient than:
if(num1 < num2 && expensiveMethodThatReturnsBool())
{
// ...
}
because &&
is a short-circuit operator. The second expression is evaluated only if the first returned true
. The same applies to ||
, the second is evaluated only if the first returned false
.
7.11 Conditional logical operators
It's similar with chained Where
s. Similar because predicates are applied to every remaining item which passed through the previous Where
.
So this...
.Where(x => expensiveMethodThatReturnsBool(x))
.Where(x => x.num1 < x.num2)
can also be less efficient than:
.Where(x => x.num1 < x.num2)
.Where(x => expensiveMethodThatReturnsBool(x))
The first is logically equivalent to:
.Where(x => expensiveMethodThatReturnsBool(x) && x.num1 < x.num2)
Well consider it yes, as all thos generated as SQL at the end the two different sql statements will be different, now in certain situations order of things in sql matter, but you cant tell for sure unless you measure it.
I suggest to noy worry about that until you face or know something for sure upon measuring performance.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With