2

How to order by StartDate and UserLikeProduct? I need to sort by StartDate but show by UserLikeProduct first.

public IEnumerable<Check> GetHomeCeeck()
{    
    return this.Query()
               .Where(c => c.IsPublish && c.IsHomepageProduct)
               .OrderBy(c => c.StartDate)
               .Take(30)
               .ToList();
}
nawfal
  • 62,042
  • 48
  • 302
  • 339
smart boy
  • 593
  • 4
  • 10
  • 23

2 Answers2

2

If I understand you correctly, you want to order by one and then the other?

use .ThenBy(lambda) after the .OrderBy(lambda).

If this is not what you meant then let me know and I'll remove this answer

.OrderBy(c => c.StartDate).ThenBy(c => c.Like)
fulvio
  • 24,168
  • 20
  • 122
  • 198
griegs
  • 22,002
  • 28
  • 113
  • 201
  • Incorrect syntax near the keyword 'asc'. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Exception Details: System.Data.SqlClient.SqlException: Incorrect syntax near the keyword 'asc'. Source Error: Line 25: { Line 26: Line 27: return this.Query().Where(c => c.IsPublish && c.IsHomepageProduct).OrderBy(c=>c.StartDate).ThenBy(c=>c.UserLikeProduct).Take(30).ToList(); Line 28: } Line 29: – smart boy May 21 '13 at 06:15
  • 1
    @smartboy What data type is 'UserLikeProduct'? Is it an IComparable? Although that may not matter since this is trying to generate SQL – TyCobb May 23 '13 at 03:47
1

See this question: Multiple Order By with LINQ

You need to OrderBy() and then ThenBy().

Community
  • 1
  • 1
TyCobb
  • 8,411
  • 1
  • 27
  • 47