Alex Rivera | Logout

Sequence of LINQ method of any importance?

Asked 2010-12-16T08:30:55.517
11

Just wondering, does it matter in which sequence the LINQ methods are added?

Eg.

using(MyDataContext context = new MyDataContext())
{
   var user = context.Users
                     .Where(u => u.UserName.StartsWith("t"))
                     .OrderByDescending(u => u.CreatedDate)
                     .FirstOrDefault();
}

and this are completely the same?

using(MyDataContext context = new MyDataContext())
{
   var user = context.Users
                     .OrderByDescending(u => u.CreatedDate)
                     .Where(u => u.UserName.StartsWith("t"))
                     .FirstOrDefault();
}

Of course I can have all methods tested one by one, but I'd like to have some general idea about the logic.

So:

  • Other than methods like FirstOrDefault(), ToList() and other methods that really trigger the execution is it of any importance to have some type of order in the LINQ statement?

Thanks again!

Edit
Report

3 Answers

10

In LINQ to SQL, I'd expect these two queries to be the same - they should end up with the same query plan, at least, even if not the exact same SQL.

In LINQ to Objects, they would behave very differently. Imagine you had a million users, but only two of them had usernames starting with "t". in the first form, you'd be filtering and then sorting those two users... in the second form, it would need to sort everything before it started filtering.

Of course there are other situations where the ordering matters too - in particular, if you have a Select half way down and then a Where clause, then you'll be filtering on different things. Imagine this:

var query = Enumerable.Range(-20, 30)
                      .Select(x => -x)
                      .Where(x => x > 5);

vs

var query = Enumerable.Range(-20, 30)
                      .Where(x => x > 5)
                      .Select(x => -x);

In the first example the results will be "20, 19, 18, ... 6" whereas in the second query the results will be "-6, -7, -8, -9, -10". Hugely different!

answered 2010-12-16T08:33:46.173
1

I'm not 100 % sure but I think the second one is slower since you do the sorting on a larger set of data. If you filter first you will remove some of the elements this making the sorting faster. However, the result should be the same.

EDIT: Since this looks like linq-to-sql (if you're not using another linq provider) it should boil down to the same query being executed in this example. But there are situations where the ordering matters in linq-to-sql as well (see Jon's example). However, the only way to be 100 % sure is to use the profiler to investigate the generated sql query (but in this example I don't think there are any difference).

answered 2010-12-16T08:33:34.520
0

It might have performance issues. In your case, the first example will be best, because on the second one you first sort the entire list before filtering. You even sort all the ones you don't need, and then you remove the unwanted parts. On the first you remove all you don't need, then you sort the sub-set that will (probably) be much smaller.

so for this exact query, the result are the same, but for a big set of data, the first one will be the fastest one.

answered 2010-12-16T08:33:50.127

Your Answer