Ascending sort
List<Product> products = Model.New<Product>().GetAll().OrderBy(p => p.Name).ToList();

Descending sort
products = Model.New<Product>().GetAll().OrderByDescending(p => p.Name).ToList();

Sort using a string instead of a lambda
products = Model.New<Product>().GetAll().OrderBy("Name").ToList();

Secondary ascending sort (all of these can accept strings or lambdas)
products = Model.New<Product>().GetAll().OrderBy(p => p.Quantity).ThenBy(p => p.Name).ToList();

Secondary descending sort
products = Model.New<Product>().GetAll().OrderBy(p => p.Quantity).ThenByDescending(p => p.Name).ToList();

Tertiary ascending sort
products = Model.New<Product>().GetAll().OrderBy(p => p.Quantity).ThenBy(p => p.Description).ThenBy(p => p.Name).ToList();

Chaining some of the above together
products = Model.New<Product>().GetAll().Where(p => p.Quantity == 99).OrderBy(p => p.Name).ToList();

Using the above with paging
IPagedList<Product> pagedProducts = Model.New<Product>().GetAll().Where(p => p.Name.StartsWith("Widget_")).OrderByDescending(p => p.Quantity).ToPagedList(3, 2);

Last edited Feb 23, 2010 at 3:04 AM by timhardy, version 6

Comments

No comments yet.