메서드 기반 쿼리 구문 예제: 변환(LINQ to Entities)
이 항목의 예제에서는 ToArray, ToDictionary 및 ToList 메서드를 사용하여 메서드 기반 쿼리 구문으로 AdventureWorks Sales 모델을 쿼리하는 방법을 보여 줍니다. 이 예제에서 사용하는 AdventureWorks Sales 모델에서는 AdventureWorks 샘플 데이터베이스의 Contact, Address, Product, SalesOrderHeader 및 SalesOrderDetail 테이블을 사용합니다.
이 항목의 예제에서는 다음과 같은 using/Imports 문을 사용합니다.
Option Explicit On
Option Strict On
Imports L2EExamplesVB.AdventureWorksModel
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 AdventureWorksModel;
using System.Globalization;
자세한 내용은 방법: Visual Studio에서 LINQ to Entities 프로젝트 만들기를 참조하십시오.
ToArray
예제
다음 예제에서는 ToArray 메서드로 시퀀스를 즉시 계산하여 배열에 넣습니다.
Using AWEntities As New AdventureWorksEntities
Dim products As ObjectQuery(Of Product) = AWEntities.Product
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 AWEntities = new AdventureWorksEntities())
{
ObjectQuery<Product> products = AWEntities.Product;
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 AWEntities As New AdventureWorksEntities
Dim products As ObjectQuery(Of Product) = AWEntities.Product
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 AWEntities = new AdventureWorksEntities())
{
ObjectQuery<Product> products = AWEntities.Product;
Dictionary<String, Product> scoreRecordsDict = products.
ToDictionary(record => record.Name);
Console.WriteLine("Top Tube's ProductID: {0}",
scoreRecordsDict["Top Tube"].ProductID);
}
ToList
예제
다음 예제에서는 ToList 메서드로 시퀀스를 즉시 계산하여 List에 넣습니다. 여기서 T는 DataRow 형식입니다.
Using AWEntities As New AdventureWorksEntities
Dim products As ObjectQuery(Of Product) = AWEntities.Product
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 AWEntities = new AdventureWorksEntities())
{
ObjectQuery<Product> products = AWEntities.Product;
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));
}
}