Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All

Query Expression Syntax Examples: Ordering

The examples in this topic demonstrate how to use the OrderBy and OrderByDescending methods to query the AdventureWorks Sales Model using query expression syntax. The AdventureWorks Sales Model used in these examples is built from the Contact, Address, Product, SalesOrderHeader, and SalesOrderDetail tables in the AdventureWorks sample database.

The examples in this topic use the following using/Imports statements:


using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Objects;
using System.Globalization;
using System.Data.EntityClient;
using System.Data.SqlClient;
using System.Data.Common;
    

OrderBy

Example

The following example uses OrderBy to return a list of contacts ordered by last name.


using (AdventureWorksEntities context = new AdventureWorksEntities())
{
IQueryable<Contact> sortedNames =
from n in context.Contacts
orderby n.LastName
select n;

Console.WriteLine("The sorted list of last names:");
foreach (Contact n in sortedNames)
{
Console.WriteLine(n.LastName);
}
}
    

Example

The following example uses OrderBy to sort a list of contacts by length of last name.


using (AdventureWorksEntities context = new AdventureWorksEntities())
{
IQueryable<Contact> sortedNames =
from n in context.Contacts
orderby n.LastName.Length
select n;

Console.WriteLine("The sorted list of last names (by length):");
foreach (Contact n in sortedNames)
{
Console.WriteLine(n.LastName);
}
}
    

OrderByDescending

Example

The following example uses orderby… descending (Order By … Descending in Visual Basic), which is equivalent to the OrderByDescending method, to sort the price list from highest to lowest.


using (AdventureWorksEntities context = new AdventureWorksEntities())
{
IQueryable<Decimal> sortedPrices =
from p in context.Products
orderby p.ListPrice descending
select p.ListPrice;

Console.WriteLine("The list price from highest to lowest:");
foreach (Decimal price in sortedPrices)
{
Console.WriteLine(price);
}
}
    

ThenBy

Example

The following example uses OrderBy and ThenBy to return a list of contacts ordered by last name and then by first name.


using (AdventureWorksEntities context = new AdventureWorksEntities())
{
IQueryable<Contact> sortedContacts =
from contact in context.Contacts
orderby contact.LastName, contact.FirstName
select contact;

Console.WriteLine("The list of contacts sorted by last name then by first name:");
foreach (Contact sortedContact in sortedContacts)
{
Console.WriteLine(sortedContact.LastName + ", " + sortedContact.FirstName);
}
}
    

ThenByDescending

Example

The following example uses OrderBy… Descending, which is equivalent to the ThenByDescending method, to sort a list of products, first by name and then by list price from highest to lowest.


using (AdventureWorksEntities context = new AdventureWorksEntities())
{
IQueryable<Product> query =
from product in context.Products
orderby product.Name, product.ListPrice descending
select product;

foreach (Product product in query)
{
Console.WriteLine("Product ID: {0} Product Name: {1} List Price {2}",
product.ProductID,
product.Name,
product.ListPrice);
}
}
    

See Also




Build Date:

2012-10-01
Show:
© 2015 Microsoft