다음을 통해 공유


메서드 기반 쿼리 구문 예제: 변환(LINQ to Entities)

이 항목의 예제에서는 ToArray, ToDictionaryToList 메서드를 사용하여 메서드 기반 쿼리 구문으로 AdventureWorks Sales 모델을 쿼리하는 방법을 보여 줍니다. 이 예제에서 사용하는 AdventureWorks Sales 모델은 AdventureWorks 샘플 데이터베이스의 Contact, Address, Product, SalesOrderHeader 및 SalesOrderDetail 테이블로 작성되었습니다.

이 항목의 예제에서는 다음 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;

ToArray

예제

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

Using context As New AdventureWorksEntities
    Dim products As ObjectSet(Of Product) = context.Products

    Dim prodArray As Product() = ( _
        From product In products _
        Order By product.ListPrice Descending _
        Select product).ToArray()

    Console.WriteLine("The list price from highest to lowest:")
    For Each prod As Product In prodArray
        Console.WriteLine(prod.ListPrice)
    Next
End Using
using (AdventureWorksEntities context = new AdventureWorksEntities())
{
    ObjectSet<Product> products = context.Products;

    Product[] prodArray = (
        from product in products
        orderby product.ListPrice descending
        select product).ToArray();

    Console.WriteLine("Every price from highest to lowest:");
    foreach (Product product in prodArray)
    {
        Console.WriteLine(product.ListPrice);
    }
}

ToDictionary

예제

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

Using context As New AdventureWorksEntities
    Dim products As ObjectSet(Of Product) = context.Products

    Dim scoreRecordsDict As Dictionary(Of String, Product) = _
        products.ToDictionary(Function(record) record.Name)

    Console.WriteLine("Top Tube's ProductID: {0}", _
        scoreRecordsDict("Top Tube").ProductID)
End Using
using (AdventureWorksEntities context = new AdventureWorksEntities())
{
    ObjectSet<Product> products = context.Products;

    Dictionary<String, Product> scoreRecordsDict = products.
            ToDictionary(record => record.Name);

    Console.WriteLine("Top Tube's ProductID: {0}",
            scoreRecordsDict["Top Tube"].ProductID);
}

ToList

예제

다음 예제에서는 ToList 메서드로 시퀀스를 즉시 계산하여 List에 넣습니다. 여기서 TDataRow 형식입니다.

Using context As New AdventureWorksEntities
    Dim products As ObjectSet(Of Product) = context.Products

    Dim prodList As List(Of Product) = ( _
        From product In products _
        Order By product.Name _
        Select product).ToList()

    Console.WriteLine("The product list, ordered by product name:")
    For Each prod As Product In prodList
        Console.WriteLine(prod.Name.ToLower(CultureInfo.InvariantCulture))
    Next
End Using
using (AdventureWorksEntities context = new AdventureWorksEntities())
{
    ObjectSet<Product> products = context.Products;

    List<Product> query =
        (from product in products
         orderby product.Name
         select product).ToList();

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

참고 항목

개념

LINQ to Entities의 쿼리