Query sorting

Query sort order can be declared in dbmodel or given before the query is loaded. If query has sort order in dbmodel you cannot change it with query sort methods.

Example of sorted query:
query ProductNames [[
  SELECT ProductID, ProductName
  FROM Products
]] order by [ ProductName ];

You must give order by column(s) in separate “order by” section. If you put order by keyword to the actual query, query will fail when filtered or paged.

A query without a sort order can be sorted with SortBy and SortDescBy extension methods.
query ProductSupplierNames [[
  SELECT ProductID, p.ProductName, s.CompanyName
  FROM Products p 
    join Suppliers s on (p.SupplierID = s.SupplierID)
]] ;

using (var ctx = SqlDataContext.CreateContext()) {
    ProductSupplierNames pn = new ProductSupplierNames();
    pn.SortBy(row => row.ProductName).SortDescBy(row => row.CompanyName);
    pn.Load(ctx);
}

Next chapter: Common business logic

Last edited Oct 26, 2010 at 9:55 AM by nolics, version 2

Comments

No comments yet.