방법: 데이터 필터링(Entity Framework)
이 항목에서는 쿼리 결과를 필터링하는 방법을 보여 줍니다. 이 예제에서는 $2,500 초과 온라인 주문을 모두 나타내는 SalesOrderHeader 개체의 컬렉션을 반환합니다. 또한 동일한 예제에서 다음의 Entity Framework 쿼리 기술을 사용한 경우도 보여 줍니다.
LINQ to Entities
ObjectQuery<T>가 포함된 Entity SQL
ObjectQuery<T>의 쿼리 작성기 메서드
이 항목의 예제는 Adventure Works Sales 모델을 기반으로 합니다. 이 항목의 코드를 실행하려면 프로젝트에 Adventure Works Sales 모델을 추가하고 프로젝트에서 Entity Framework를 사용하도록 구성해야 합니다. 자세한 내용은 방법: 엔터티 데이터 모델 마법사 사용(Entity Framework) 또는 방법: Entity Framework 프로젝트 수동 구성 및 방법: 엔터티 데이터 모델 수동 정의(Entity Framework)를 참조하십시오.
예제
다음은 LINQ to Entities 예제입니다.
Using context As New AdventureWorksEntities()
' Specify the order amount.
Dim orderCost As Integer = 2500
' Define a LINQ query that returns only online orders
' more than the specified amount.
Dim onlineOrders = From order In context.SalesOrderHeaders _
Where order.OnlineOrderFlag = True AndAlso order.TotalDue > orderCost _
Select order
' Print order information.
For Each onlineOrder In onlineOrders
Console.WriteLine("Order ID: {0} Order date: ", onlineOrder.SalesOrderID)
Console.WriteLine("Order date: {0:d}", onlineOrder.OrderDate)
Console.WriteLine("Order number: {0}", onlineOrder.SalesOrderNumber)
Next
End Using
using (AdventureWorksEntities context = new AdventureWorksEntities())
{
// Specify the order amount.
int orderCost = 2500;
// Define a LINQ query that returns only online orders
// more than the specified amount.
var onlineOrders =
from order in context.SalesOrderHeaders
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);
}
}
다음은 Entity SQL 예제입니다.
Using context As New AdventureWorksEntities()
' Specify the order amount.
Dim orderCost As Decimal = 2500
' Specify the Entity SQL query that returns only online orders
' more than the specified amount.
Dim queryString As String = "SELECT VALUE o FROM AdventureWorksEntities.SalesOrderHeaders AS o " & _
" WHERE o.OnlineOrderFlag = TRUE AND o.TotalDue > @ordercost"
' Define an ObjectQuery and pass the maxOrderCost parameter.
Dim onlineOrders As 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
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 AdventureWorksEntities.SalesOrderHeaders AS o
WHERE o.OnlineOrderFlag = TRUE AND o.TotalDue > @ordercost";
// 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);
}
}
다음은 쿼리 작성기 메서드 예제입니다.
Using context As New AdventureWorksEntities()
' Specify the order amount.
Dim orderCost As Integer = 2500
' Define an ObjectQuery that returns only online orders
' more than the specified amount.
Dim onlineOrders As ObjectQuery(Of SalesOrderHeader) = _
context.SalesOrderHeaders.Where("it.OnlineOrderFlag = TRUE AND 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
End Using
using (AdventureWorksEntities context = new AdventureWorksEntities())
{
// Specify the order amount.
int orderCost = 2500;
// Define an ObjectQuery that returns only online orders
// more than the specified amount.
ObjectQuery<SalesOrderHeader> onlineOrders =
context.SalesOrderHeaders
.Where("it.OnlineOrderFlag = TRUE AND it.TotalDue > @ordercost",
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);
}
}