다음을 통해 공유


쿼리 식 구문 예제: 분할(LINQ to DataSet)

이 항목의 예제에서는 SkipTake 메서드에서 쿼리 식 구문을 사용하여 DataSet을 쿼리하는 방법을 보여 줍니다.

이러한 예제에서 사용되는 FillDataSet 메서드는 데이터집합에 데이터를 로드하는데 지정됩니다.

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

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

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Globalization;
using System.Linq;
using System.Windows.Forms;
Option Explicit On

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

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

Skip

예시

이 예제에서는 Skip 메서드를 사용하여 Seattle에 있는 처음 2개의 주소를 제외한 모든 주소를 가져옵니다.

// Fill the DataSet.
DataSet ds = new DataSet();
ds.Locale = CultureInfo.InvariantCulture;
FillDataSet(ds);

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

var query = (
    from address in addresses.AsEnumerable()
    from order in orders.AsEnumerable()
    where address.Field<int>("AddressID") == order.Field<int>("BillToAddressID")
         && address.Field<string>("City") == "Seattle"
    select new
    {
        City = address.Field<string>("City"),
        OrderID = order.Field<int>("SalesOrderID"),
        OrderDate = order.Field<DateTime>("OrderDate")
    }).Skip(2);

Console.WriteLine("All but first 2 orders in Seattle:");
foreach (var order in query)
{
    Console.WriteLine("City: {0} Order ID: {1} Total Due: {2:d}",
        order.City, order.OrderID, order.OrderDate);
}
' 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 addresses As DataTable = ds.Tables("Address")
Dim orders As DataTable = ds.Tables("SalesOrderHeader")

Dim query = ( _
    From address In addresses.AsEnumerable() _
    From order In orders.AsEnumerable() _
    Where (address.Field(Of Integer)("AddressID") = _
           order.Field(Of Integer)("BillToAddressID")) _
         And address.Field(Of String)("City") = "Seattle" _
    Select New With _
       { _
           .City = address.Field(Of String)("City"), _
           .OrderID = order.Field(Of Integer)("SalesOrderID"), _
           .OrderDate = order.Field(Of DateTime)("OrderDate") _
       }).Skip(2)

Console.WriteLine("All but first 2 orders in Seattle:")
For Each addOrder In query
    Console.Write("City: " & addOrder.City)
    Console.Write(" Order ID: " & addOrder.OrderID)
    Console.WriteLine(" Order date: " & addOrder.OrderDate)
Next

Take

예시

이 예제에서는 Take 메서드를 사용하여 Seattle에 있는 처음 3개의 주소를 가져옵니다.

// Fill the DataSet.
DataSet ds = new DataSet();
ds.Locale = CultureInfo.InvariantCulture;
FillDataSet(ds);

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

var query = (
    from address in addresses.AsEnumerable()
    from order in orders.AsEnumerable()
    where address.Field<int>("AddressID") == order.Field<int>("BillToAddressID")
         && address.Field<string>("City") == "Seattle"
    select new
    {
        City = address.Field<string>("City"),
        OrderID = order.Field<int>("SalesOrderID"),
        OrderDate = order.Field<DateTime>("OrderDate")
    }).Take(3);

Console.WriteLine("First 3 orders in Seattle:");
foreach (var order in query)
{
    Console.WriteLine("City: {0} Order ID: {1} Total Due: {2:d}",
        order.City, order.OrderID, order.OrderDate);
}
' 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 addresses As DataTable = ds.Tables("Address")
Dim orders As DataTable = ds.Tables("SalesOrderHeader")

Dim query = ( _
    From address In addresses.AsEnumerable() _
    From order In orders.AsEnumerable() _
    Where (address.Field(Of Integer)("AddressID") = _
        order.Field(Of Integer)("BillToAddressID")) _
         And address.Field(Of String)("City") = "Seattle" _
    Select New With _
    { _
        .City = address.Field(Of String)("City"), _
        .OrderID = order.Field(Of Integer)("SalesOrderID"), _
        .OrderDate = order.Field(Of DateTime)("OrderDate") _
    }).Take(3)

Console.WriteLine("First 3 orders in Seattle:")
For Each order In query
    Console.Write("City: " & order.City)
    Console.Write(" Order ID: " & order.OrderID)
    Console.WriteLine(" Order date: " & order.OrderDate)
Next

참고 항목