다음을 통해 공유


메서드 기반 쿼리 구문 예제: 변환 연산자(LINQ to DataSet)

업데이트: November 2007

이 항목의 예제에서는 ToArray<TSource>, ToDictionaryToList<TSource> 메서드를 사용하여 쿼리 식을 즉시 실행합니다.

이러한 예제에 사용된 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 프로젝트 만들기를 참조하십시오.

ToArray

예제

이 예제에서는 ToArray<TSource> 메서드로 시퀀스를 즉시 계산하여 배열에 넣습니다.

' 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 productsArray = products.AsEnumerable().ToArray()

Dim query = _
        From product In productsArray _
        Select product _
        Order By product.Field(Of Decimal)("ListPrice") Descending

Console.WriteLine("Every price From highest to lowest:")
For Each product In query
    Console.WriteLine(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> productsArray = products.AsEnumerable().ToArray();

IEnumerable<DataRow> query =
    from product in productsArray
    orderby product.Field<Decimal>("ListPrice") descending
    select product;

Console.WriteLine("Every price from highest to lowest:");
foreach (DataRow product in query)
{
    Console.WriteLine(product.Field<Decimal>("ListPrice"));
}

ToDictionary

예제

이 예제에서는 ToDictionary 메서드로 시퀀스 및 관련 키 식을 즉시 계산하여 사전에 넣습니다.

' 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 scoreRecordsDict = products.AsEnumerable(). _
    ToDictionary(Function(record) record.Field(Of String)("Name"))

Console.WriteLine("Top Tube's ProductID: {0}", _
    scoreRecordsDict("Top Tube")("ProductID"))
// Fill the DataSet.
DataSet ds = new DataSet();
ds.Locale = CultureInfo.InvariantCulture;
FillDataSet(ds);

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


var scoreRecordsDict = products.AsEnumerable().
    ToDictionary(record => record.Field<string>("Name"));
Console.WriteLine("Top Tube's ProductID: {0}",
    scoreRecordsDict["Top Tube"]["ProductID"]);

ToList

예제

이 예제에서는 ToList<TSource> 메서드로 시퀀스를 즉시 계산하여 List<T>에 넣습니다. 여기서 T는 DataRow 형식입니다.

' 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 productList = products.AsEnumerable().ToList()

Dim query = _
    From product In productList _
    Select product _
    Order By product.Field(Of String)("Name")

Console.WriteLine("The sorted name list:")

For Each product In query
    Console.WriteLine(product.Field(Of String)("Name").ToLower(CultureInfo.InvariantCulture))
Next
// Fill the DataSet.
DataSet ds = new DataSet();
ds.Locale = CultureInfo.InvariantCulture;
FillDataSet(ds);

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

IEnumerable<DataRow> productList = products.AsEnumerable().ToList();

IEnumerable<DataRow> query =
    from product in productList
    orderby product.Field<string>("Name")
    select product;

Console.WriteLine("The product list, ordered by product name:");
foreach (DataRow product in query)
{
    Console.WriteLine(product.Field<string>("Name").ToLower(CultureInfo.InvariantCulture));
}

참고 항목

개념

DataSet에 데이터 로드

표준 쿼리 연산자 개요

기타 리소스

LINQ to DataSet 예제