방법: 데이터 필터링(Entity Framework)
이 항목에서는 쿼리 결과를 필터링하는 방법을 보여 줍니다. 이 예제에서는 $2,500 초과 온라인 주문을 모두 나타내는 SalesOrderHeader 개체의 컬렉션을 반환합니다. 다음의 엔터티 프레임워크 쿼리 기술을 각각 사용하여 동일한 예제를 보여 줍니다.
LINQ to Entities
ObjectQuery<T>를 사용한 Entity SQL
ObjectQuery<T>의 쿼리 작성기 메서드
이 항목의 예제는 Adventure Works Sales 모델을 기반으로 합니다. 이 예제의 코드를 실행하려면 프로젝트에 AdventureWorks Sales 모델을 추가하고 엔터티 프레임워크를 사용하도록 프로젝트를 구성해야 합니다. 이렇게 하려면 방법: Entity Framework 프로젝트 수동 구성 및 방법: 엔터티 데이터 모델 수동 정의(Entity Framework)의 절차를 수행합니다. 엔터티 데이터 모델 마법사를 사용하여 AdventureWorks Sales 모델을 정의할 수도 있습니다. 자세한 내용은 방법: 엔터티 데이터 모델 마법사 사용(Entity Framework)을 참조하십시오.
예제
다음은 LINQ to Entities 예제입니다.
Using context As AdventureWorksEntities = _
New AdventureWorksEntities()
' Specify the order amount.
Dim orderCost As Integer = 2500
Try
' Define a LINQ query that returns only online orders
' more than the specified amount.
Dim onlineOrders = _
From order As SalesOrderHeader In context.SalesOrderHeader() _
Where order.OnlineOrderFlag = True And _
order.TotalDue > orderCost _
Select order
' Print order information.
For Each onlineOrder In onlineOrders
Console.WriteLine("Order ID:{0}Order date: " _
+ "{1}:d Order number: {2}", _
onlineOrder.SalesOrderID, _
onlineOrder.OrderDate, _
onlineOrder.SalesOrderNumber)
Next
Catch ex As EntitySqlException
Console.WriteLine(ex.ToString())
End Try
End Using
using (AdventureWorksEntities context = new AdventureWorksEntities())
{
// Specify the order amount.
int orderCost = 2500;
try
{
// Define a LINQ query that returns only online orders
// more than the specified amount.
var onlineOrders =
from order in context.SalesOrderHeader
where order.OnlineOrderFlag == true &&
order.TotalDue > orderCost
select order;
// Print order information.
foreach (var onlineOrder in onlineOrders)
{
Console.WriteLine("Order ID: {0} Order date: "
+ "{1:d} Order number: {2}",
onlineOrder.SalesOrderID,
onlineOrder.OrderDate,
onlineOrder.SalesOrderNumber);
}
}
catch (EntitySqlException ex)
{
Console.WriteLine(ex.ToString());
}
}
다음은 Entity SQL 예제입니다.
Using context As AdventureWorksEntities = _
New AdventureWorksEntities()
' Specify the order amount.
Dim orderCost As Integer = 2500
' Specify the Entity SQL query that returns only online orders
' more than the specified amount.
Dim queryString As String = "SELECT VALUE o FROM SalesOrderHeader AS o " _
+ "WHERE o.OnlineOrderFlag = TRUE AND o.TotalDue > @ordercost"
Try
' Define an ObjectQuery and pass the maxOrderCost parameter.
Dim onlineOrders As ObjectQuery(Of SalesOrderHeader) = _
New ObjectQuery(Of SalesOrderHeader)(queryString, context)
onlineOrders.Parameters.Add( _
New ObjectParameter("ordercost", orderCost))
' Print order information.
For Each onlineOrder In onlineOrders
Console.WriteLine("Order ID:{0}Order date: " _
+ "{1}:d Order number: {2}", _
onlineOrder.SalesOrderID, _
onlineOrder.OrderDate, _
onlineOrder.SalesOrderNumber)
Next
Catch ex As EntitySqlException
Console.WriteLine(ex.ToString())
End Try
End Using
using (AdventureWorksEntities context = new AdventureWorksEntities())
{
// Specify the order amount.
decimal orderCost = 2500;
// Specify the Entity SQL query that returns only online orders
// more than the specified amount.
string queryString = @"SELECT VALUE o FROM SalesOrderHeader AS o
WHERE o.OnlineOrderFlag = TRUE AND o.TotalDue > @ordercost";
try
{
// Define an ObjectQuery and pass the maxOrderCost parameter.
ObjectQuery<SalesOrderHeader> onlineOrders =
new ObjectQuery<SalesOrderHeader>(queryString, context);
onlineOrders.Parameters.Add(
new ObjectParameter("ordercost", orderCost));
// Print order information.
foreach (var onlineOrder in onlineOrders)
{
Console.WriteLine("Order ID: {0} Order date: "
+ "{1:d} Order number: {2}",
onlineOrder.SalesOrderID,
onlineOrder.OrderDate,
onlineOrder.SalesOrderNumber);
}
}
catch (EntitySqlException ex)
{
Console.WriteLine(ex.ToString());
}
}
다음은 쿼리 작성기 방법을 사용한 예제입니다.
Using context As AdventureWorksEntities = _
New AdventureWorksEntities()
' Specify the order amount.
Dim orderCost As Integer = 2500
Try
' Define an ObjectQuery that returns only online orders
' more than the specified amount.
Dim onlineOrders As ObjectQuery(Of SalesOrderHeader) = _
context.SalesOrderHeader.Where("it.OnlineOrderFlag = TRUE") _
.Where("it.TotalDue > @ordercost", _
New ObjectParameter("ordercost", orderCost))
' Print order information.
For Each onlineOrder In onlineOrders
Console.WriteLine("Order ID:{0}Order date: " _
+ "{1}:d Order number: {2}", _
onlineOrder.SalesOrderID, _
onlineOrder.OrderDate, _
onlineOrder.SalesOrderNumber)
Next
Catch ex As EntitySqlException
Console.WriteLine(ex.ToString())
End Try
End Using
using (AdventureWorksEntities context = new AdventureWorksEntities())
{
// Specify the order amount.
int orderCost = 2500;
try
{
// Define an ObjectQuery that returns only online orders
// more than the specified amount.
ObjectQuery<SalesOrderHeader> onlineOrders =
context.SalesOrderHeader
.Where("it.OnlineOrderFlag = TRUE AND it.TotalDue > @ordercost",
new ObjectParameter("ordercost", orderCost));
// Print order information.
foreach (var onlineOrder in onlineOrders)
{
var items = (from item in context.SalesOrderDetail
where item.SalesOrderHeader == onlineOrder
select item).Take(3);
Console.WriteLine("Order ID: {0} Order date: "
+ "{1:d} Order number: {2}",
onlineOrder.SalesOrderID,
onlineOrder.OrderDate,
onlineOrder.SalesOrderNumber);
}
}
catch (EntitySqlException ex)
{
Console.WriteLine(ex.ToString());
}
}