Reputation: 1875
I'm trying to come up with a LINQ query that will return the first row with the most hits, followed by the rest of the rows sorted by date desc. Is this possible to do in SQL or would i need 2 separate queries? My table looks similar to this.
Backend: SQL Server 2008
Table Name: Products
ProductID ProductName Hits DateAdded
1 Coffee 600 5/2/2012
2 Vanilla 800 4/19/2012
3 Chocolate 3000 3/25/2012
4 Oreo 200 2/10/2012
5 Heath 250 5/13/2012
6 Sherbet 550 4/20/2012
7 Mocha 2000 3/22/2012
I would like the results of the query to come back as such: (Chocolate first - based off hits, the rest - order by date descending)
3 Chocolate 3000 3/25/2012
5 Heath 250 5/13/2012
1 Coffee 600 5/2/2012
6 Sherbet 550 4/20/2012
2 Vanilla 800 4/19/2012
7 Mocha 2000 3/22/2012
4 Oreo 200 2/10/2012
Any help would be greatly appreciated. I'm a LINQ rookie.
Thanks!
Upvotes: 1
Views: 97
Reputation: 43011
int maxHits = Products.Max( p => p.Hits );
var query = Products.OrderByDescending(
p => p.Hits == maxHits ).ThenByDescending( p => p.DateAdded );
Upvotes: 1