Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does order of conditions under where clause in a LINQ query matter

Tags:

c#

linq

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.

like image 564
Mahesh Avatar asked Aug 20 '14 06:08

Mahesh


People also ask

What is default order by in LINQ?

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.

Can we use multiple where clause in LINQ?

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.

How do you order alphabetically in LINQ?

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.

Why from comes before select in LINQ?

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.

Does order matter in SQL Server where condition?

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.

What are the where conditions in LINQ and Lambda queries?

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.

How to measure the performance of where clause in SQL?

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.

When changing the Order of columns in where condition result set?

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.


2 Answers

If it's LINQ-To-Objects it matters.

Enumerable.Where is comparable to an ifclause. 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 Wheres. 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)
like image 170
Tim Schmelter Avatar answered Oct 31 '22 16:10

Tim Schmelter


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.

like image 31
Samer Aburabie Avatar answered Oct 31 '22 17:10

Samer Aburabie