メソッド ベースのクエリ構文例 : 並べ替え (LINQ to Entities)
The examples in this topic demonstrate how to use the ThenBy method to query the AdventureWorks Sales Model using method-based query 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.
このトピックの例には、次の using/Imports ステートメントが使用されています。
Option Explicit On
Option Strict On
Imports System.Data.Objects
Imports System.Globalization
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;
ThenBy
例
次の例では、メソッド ベースのクエリ構文で OrderBy と ThenBy を使用し、姓の順、次に名の順に並べ替えられた連絡先の一覧を返します。
Using context As New AdventureWorksEntities
Dim sortedContacts = context.Contacts _
.OrderBy(Function(c) c.LastName) _
.ThenBy(Function(c) c.FirstName) _
.Select(Function(c) c)
Console.WriteLine("The list of contacts sorted by last name then by first name:")
For Each sortedContact As Contact In sortedContacts
Console.WriteLine(sortedContact.LastName + ", " + sortedContact.FirstName)
Next
End Using
using (AdventureWorksEntities context = new AdventureWorksEntities())
{
IQueryable<Contact> sortedContacts = context.Contacts
.OrderBy(c => c.LastName)
.ThenBy(c => c.FirstName);
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
例
次の例では、OrderBy メソッドおよび ThenByDescending メソッドを使用し、最初に表示価格で並べ替えた後、製品名の降順で並べ替えます。
Using context As New AdventureWorksEntities
Dim products As ObjectSet(Of Product) = context.Products
Dim query As IOrderedQueryable(Of Product) = products _
.OrderBy(Function(prod As Product) prod.ListPrice) _
.ThenByDescending(Function(prod As Product) prod.Name)
For Each prod As Product In query
Console.WriteLine("Product ID: {0} Product Name: {1} List Price {2}", _
prod.ProductID, _
prod.Name, _
prod.ListPrice)
Next
End Using
using (AdventureWorksEntities context = new AdventureWorksEntities())
{
IOrderedQueryable<Product> query = context.Products
.OrderBy(product => product.ListPrice)
.ThenByDescending(product => product.Name);
foreach (Product product in query)
{
Console.WriteLine("Product ID: {0} Product Name: {1} List Price {2}",
product.ProductID,
product.Name,
product.ListPrice);
}
}