0

I have the following query:

var vendors = (from pp in this.ProductPricings
               join pic in this.ProductItemCompanies
               on pp.CompanyId equals pic.CompanyId into left
               from pic in left.DefaultIfEmpty()
               orderby pp.EffectiveDate descending
               group pp by new { pp.Company, SortOrder = (pic != null) ? pic.SortOrder : short.MinValue } into v
               select v).OrderBy(z => z.Key.SortOrder);

Does anyone know how the last OrderBy() is applied? Does that become part of the SQL query, or are all the results loaded in to memory and then passed to OrderBy()?

And if it's the second case, is there any way to make it all one query? I only need the first item and it would be very inefficent to return all the results.

Jonathan Wood
  • 65,341
  • 71
  • 269
  • 466
  • 1
    Look at a tool like EFProfiler, it will show you what the generated query looks like. You're not enumerating, so my understanding is that the order by will become part of the generated SQL statement. – tbddeveloper Sep 03 '14 at 21:15
  • The generated SQL should tell you what is going on. http://stackoverflow.com/questions/1412863/how-do-i-view-the-sql-generated-by-the-entity-framework – mxmissile Sep 03 '14 at 21:15
  • The DBMS may well do what you're trying to avoid i.e. return all results then order them. If the query is sufficiently complex, then SQL Server will multithread the query and then have to re-order the entire result set before returning it, as per [this article](http://blogs.msdn.com/b/conor_cunningham_msft/archive/2008/08/27/no-seatbelt-expecting-order-without-order-by.aspx) – dyson Sep 03 '14 at 21:25

3 Answers3

1

Well it will try to apply the OrderBy to the original query since you are still using an IQueryable - meaning it hasn't been converted to an IEnumerable or hydrated to a collection using ToList or an equivalent.

Whether it can or not depends on the complexity of the resulting query. You'd have to try it to find out. My guess is it will turn the main query into a subquery and layer on a "SELECT * FROM (...) ORDER BY SortOrder" outer query.

D Stanley
  • 149,601
  • 11
  • 178
  • 240
1

Given your specific example the order by in this situation most, likely be appliead as part of the expression tree when it getting build, there for it will be applied to sql generated by the LINQ query, if you would convert it to Enumarable like ToList as mentioned in another answer then Order by would be applied as an extension to Enumerable.

COLD TOLD
  • 13,513
  • 3
  • 35
  • 52
0

Might use readable code, because as you write it is not understandable. You will have a problem in the future with the linq statement. The problem is that if your statement does not return any value the value will be null and whenever you make cause a exception.

You must be careful. I recommend you to do everything separately to understand the code friend.

icalderond
  • 19
  • 5
  • I think you are wrong. I don't see any place that a null exception can be raised here. Do you? And I also don't know what you find so unreadable. Your code looks just like mine. Did you have a better way to write it? – Jonathan Wood Sep 04 '14 at 15:51
  • I talk for my experence. I used Linq to SQL and this was my problem. And the redeable code is easly understandable. This dont want to say that i have the answer friend. – icalderond Sep 04 '14 at 17:57
  • Well, thanks. But you didn't answer either question. I have no usable information from your comment. I think you're just wrong about the null exception. But would be open to you showing me that I am wrong. – Jonathan Wood Sep 04 '14 at 18:13