메서드 기반 쿼리 구문 예제: 변환
이 항목의 예제에서는ToArray, ToDictionary 및 ToList 메서드를 사용하여 메서드 기반 쿼리 구문으로 AdventureWorks Sales 모델을 쿼리하는 방법을 보여 줍니다. 이 예제에서 사용하는 AdventureWorks Sales 모델에서는 AdventureWorks 샘플 데이터베이스의 Contact, Address, Product, SalesOrderHeader 및 SalesOrderDetail 테이블을 사용합니다.
이 항목의 예제에서는 다음과 같은 using
/Imports
문을 사용합니다.
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;
Option Explicit On
Option Strict On
Imports System.Data.Objects
Imports System.Globalization
ToArray
예시
다음 예제에서는 ToArray 메서드로 시퀀스를 즉시 계산하여 배열에 넣습니다.
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);
}
}
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
ToDictionary
예시
다음 예제에서는 ToDictionary 메서드로 시퀀스 및 관련 키 식을 즉시 계산하여 사전에 넣습니다.
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);
}
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
ToList
예시
다음 예제에서는 ToList 메서드로 시퀀스를 즉시 계산하여 List<T>에 넣습니다. 여기서 T
는 DataRow 형식입니다.
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));
}
}
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