Skip to content

Sorting

Wouter Demuynck edited this page May 15, 2016 · 2 revisions

Popsql supports sorting on SELECT statements with the OrderBy method. Multiple calls to the OrderBy method can be chained to add sorting on multiple columns.

The following example shows how to add an ORDER BY clause to a SELECT statement:

var sql = Sql
    .Select("Id", "Title", "CreatedBy", "CreatedOn")
    .From("Post")
    .OrderBy("CreatedOn", SqlSortOrder.Descending)
    .OrderBy("Title")
    .ToSql();

Which results in the following SQL statement:

SELECT [Id], [Title], [CreatedBy], [CreatedOn] FROM [Post] 
ORDER BY [CreatedOn] DESC, [Title]

The default sorting order is ascending, but you can sort in descending order by specifying SqlSortOrder.Descending in your call to the OrderBy method.

Clone this wiki locally