That really depends on the LINQ provider (the class that implements IQueryable<T>
).
On Entity Framework and LINQ to SQL (and other database related LINQ providers), the query is converted to SQL statements, and then will run on the database engine. For example, this query:
var ns = names.OrderBy(n => n).Where(n => n.Length == 5);
when iterated over, will be converted to:
SELECT * FROM names WHERE LEN(name) == 5 ORDER BY name
no matter where you put the OrderBy
clause.
So in this case, there is no performance hit. But when using LINQ to Objects (as in your example), the two variations have a lot of difference in performance. This answer form Jon Skeet covers this case pretty well.