다음을 통해 공유


메서드 기반 쿼리 구문 예제: 정렬(LINQ to DataSet)

업데이트: November 2007

이 항목의 예제에서는 OrderBy, Reverse<TSource>ThenBy 메서드에서 메서드 쿼리 구문을 사용하여 DataSet을 쿼리하고 결과를 정렬하는 방법을 보여 줍니다.

이러한 예제에 사용된 FillDataSet 메서드에 대한 자세한 내용은 DataSet에 데이터 로드를 참조하십시오.

이 항목의 예제에서는 AdventureWorks 샘플 데이터베이스의 Contact, Address, Product, SalesOrderHeader 및 SalesOrderDetail 테이블을 사용합니다.

이 항목의 예제에서는 다음과 같은 using/Imports 문을 사용합니다.

Option Explicit On

Imports System
Imports System.Linq
Imports System.Linq.Expressions
Imports System.Collections.Generic
Imports System.Data
Imports System.Data.SqlClient
Imports System.Data.Common
Imports System.Globalization
using System;
using System.Linq;
using System.Linq.Expressions;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Data.Common;
using System.Globalization;

자세한 내용은 방법: Visual Studio에서 LINQ to DataSet 프로젝트 만들기를 참조하십시오.

OrderBy

예제

이 예제에서는 OrderBy 메서드를 사용자 지정 비교자와 함께 사용하여 대소문자 구분 없이 성을 정렬합니다.

' Fill the DataSet.
Dim ds As New DataSet()
ds.Locale = CultureInfo.InvariantCulture
' See the FillDataSet method in the Loading Data Into a DataSet topic.
FillDataSet(ds)

Dim contacts As DataTable = ds.Tables("Contact")

Dim query As IEnumerable(Of DataRow) = _
    contacts.AsEnumerable().OrderBy(Function(contact) _
                contact.Field(Of String)("LastName"), _
                New CaseInsensitiveComparer())

For Each contact As DataRow In query
    Console.WriteLine(contact.Field(Of String)("LastName"))
Next
// Fill the DataSet.
DataSet ds = new DataSet();
ds.Locale = CultureInfo.InvariantCulture;
FillDataSet(ds);

DataTable contacts = ds.Tables["Contact"];

IEnumerable<DataRow> query =
    contacts.AsEnumerable().OrderBy(contact => contact.Field<string>("LastName"),
                                    new CaseInsensitiveComparer());

foreach (DataRow contact in query)
{
    Console.WriteLine(contact.Field<string>("LastName"));
}

Reverse

예제

이 예제에서는 Reverse<TSource> 메서드를 사용하여 OrderDate가 2002년 2월 20일 이전인 주문 목록을 만듭니다.

' Fill the DataSet.
Dim ds As New DataSet()
ds.Locale = CultureInfo.InvariantCulture
' See the FillDataSet method in the Loading Data Into a DataSet topic.
FillDataSet(ds)

Dim orders As DataTable = ds.Tables("SalesOrderHeader")

Dim query = ( _
    From order In orders.AsEnumerable() _
    Where order.Field(Of DateTime)("OrderDate") < New DateTime(2002, 2, 20) _
    Select order).Reverse()

Console.WriteLine("A backwards list of orders where OrderDate < Feb 20, 2002")

For Each order In query
    Console.WriteLine(order.Field(Of DateTime)("OrderDate"))
Next
// Fill the DataSet.
DataSet ds = new DataSet();
ds.Locale = CultureInfo.InvariantCulture;
FillDataSet(ds);

DataTable orders = ds.Tables["SalesOrderHeader"];

IEnumerable<DataRow> query = (
    from order in orders.AsEnumerable()
    where order.Field<DateTime>("OrderDate") < new DateTime(2002, 02, 20)
    select order).Reverse();

Console.WriteLine("A backwards list of orders where OrderDate < Feb 20, 2002");
foreach (DataRow order in query)
{
    Console.WriteLine(order.Field<DateTime>("OrderDate"));
}

ThenBy

예제

이 예제에서는 OrderByThenBy 메서드를 사용자 지정 비교자와 함께 사용하여 먼저 가격을 기준으로 정렬한 다음 대소문자 구분 없이 제품 이름을 내림차순으로 정렬합니다.

' Fill the DataSet.
Dim ds As New DataSet()
ds.Locale = CultureInfo.InvariantCulture
' See the FillDataSet method in the Loading Data Into a DataSet topic.
FillDataSet(ds)

Dim products As DataTable = ds.Tables("Product")

Dim query As IEnumerable(Of DataRow) = _
    products.AsEnumerable().OrderBy(Function(product) product.Field(Of Decimal)("ListPrice")) _
        .ThenBy(Function(product) product.Field(Of String)("Name"), _
                New CaseInsensitiveComparer())

For Each product As DataRow In query
    Console.WriteLine("Product ID: {0} Product Name: {1} List Price {2}", _
        product.Field(Of Integer)("ProductID"), _
        product.Field(Of String)("Name"), _
        product.Field(Of Decimal)("ListPrice"))
Next
// Fill the DataSet.
DataSet ds = new DataSet();
ds.Locale = CultureInfo.InvariantCulture;
FillDataSet(ds);

DataTable products = ds.Tables["Product"];

IEnumerable<DataRow> query =
    products.AsEnumerable().OrderBy(product => product.Field<Decimal>("ListPrice"))
    .ThenBy(product => product.Field<string>("Name"),
            new CaseInsensitiveComparer());

foreach (DataRow product in query)
{
    Console.WriteLine("Product ID: {0} Product Name: {1} List Price {2}",
        product.Field<int>("ProductID"),
        product.Field<string>("Name"),
        product.Field<Decimal>("ListPrice"));
}

참고 항목

개념

DataSet에 데이터 로드

표준 쿼리 연산자 개요

기타 리소스

LINQ to DataSet 예제