- This will NOT generate the Order By
1: var test = (from c in db.CreditRequests
2: where c.DateRejected != null
3: orderby c.DateRejected
4: select c).Distinct();
- This WILL generate the Order By
1: var test = (from c in db.CreditRequests
2: where c.DateRejected != null
3: orderby c.DateRejected
4: select c);
A way around this is to use Lamda Expressions
1: var test = (from c in db.CreditRequests
2: where c.DateRejected != null
3: select c).Distinct().OrderBy(d => d.DateRejected).OrderBy(d => d.CreditRequestID);
5 comments:
I just stumpled over the same issue - do you have any idea why LINQ to SQL handles it that way?
This was extremely helpful, thank you!! The Lambda trick worked. In my case the problem was partially hidden by a .Skip(x) function that apparently forces an ORDER BY to be inserted into the SQL query, but the ORDER BY clause isn't inserted if x == 0, so only my very first result was wrong.
It's actually the order that counts, not whether or not it's a Lambda:
http://programminglinq.com/blogs/marcorusso/archive/2008/07/20/use-of-distinct-and-orderby-in-linq.aspx
Ah! Now I get it, thanks :) Learning that I could dump my generated SQL query as a string from my var variable to see how LINQ converts to SQL was a key step for me too :)
Thanks. Found this helpful.
Post a Comment