Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does the order of LINQ functions matter?

People also ask

Which of the following statement is correct about order of from and select clauses in LINQ *?

No, because the order determines what the result is. In SQL (a declarative language), SELECT always comes before WHERE , which comes before GROUP BY , etc., and the parsing engine turns that into an execution plan which will execute in whatever order the optimizer thinks is best.

Is LINQ faster than for each?

No, LINQ iterators are not and will never be faster than foreach .

What is the difference between first () and take 1 in LINQ?

First will query Take 1, so there is no difference in query. Calling FirstOrDefault will be one step statement, because Take returns IEnumerable do you will need to call First anyway. First will throw exception so FirstOrDefault is always preferred.

What order are LINQ keywords in to perform a query?

In a LINQ query, the first step is to specify the data source. In C# as in most programming languages a variable must be declared before it can be used. In a LINQ query, the from clause comes first in order to introduce the data source ( customers ) and the range variable ( cust ).


It will depend on the LINQ provider in use. For LINQ to Objects, that could certainly make a huge difference. Assume we've actually got:

var query = myCollection.OrderBy(item => item.CreatedDate)
                        .Where(item => item.Code > 3);

var result = query.Last();

That requires the whole collection to be sorted and then filtered. If we had a million items, only one of which had a code greater than 3, we'd be wasting a lot of time ordering results which would be thrown away.

Compare that with the reversed operation, filtering first:

var query = myCollection.Where(item => item.Code > 3)
                        .OrderBy(item => item.CreatedDate);

var result = query.Last();

This time we're only ordering the filtered results, which in the sample case of "just a single item matching the filter" will be a lot more efficient - both in time and space.

It also could make a difference in whether the query executes correctly or not. Consider:

var query = myCollection.Where(item => item.Code != 0)
                        .OrderBy(item => 10 / item.Code);

var result = query.Last();

That's fine - we know we'll never be dividing by 0. But if we perform the ordering before the filtering, the query will throw an exception.


Yes.

But exactly what that performance difference is depends on how the underlying expression tree is evaluated by the LINQ provider.

For instance, your query may well execute faster the second time (with the WHERE clause first) for LINQ-to-XML, but faster the first time for LINQ-to-SQL.

To find out precisely what the performance difference is, you'll most likely want to profile your application. As ever with such things, though, premature optimisation is not usually worth the effort -- you may well find issues other than LINQ performance are more important.


In your particular example it can make a difference to the performance.

First query: Your OrderBy call needs to iterate through the entire source sequence, including those items where Code is 3 or less. The Where clause then also needs to iterate the entire ordered sequence.

Second query: The Where call limits the sequence to only those items where Code is greater than 3. The OrderBy call then only needs to traverse the reduced sequence returned by the Where call.


In Linq-To-Objects:

Sorting is rather slow and uses O(n) memory. Where on the other hand is relatively fast and uses constant memory. So doing Where first will be faster, and for large collections significantly faster.

The reduced memory pressure can be significant too, since allocations on the large object heap(together with their collection) are relatively expensive in my experience.