HOW TO:篩選資料 (Entity Framework)
本主題將說明如何篩選查詢結果。 此範例會傳回代表 $2,500 以上之所有線上訂單的 SalesOrderHeader 物件集合。 同時使用下列 Entity Framework 查詢技術顯示相同範例:
LINQ to Entities
Entity SQL with ObjectQuery<T>
Query builder methods of ObjectQuery<T>
本主題的範例根據 Adventure Works Sales Model。若要執行此主題中的程式碼,您必須已經將 Adventure Works Sales Model 加入到專案中,並設定您的專案使用 Entity Framework。如需詳細資訊,請參閱 HOW TO:使用實體資料模型精靈 (Entity Framework) 或 HOW TO:手動設定 Entity Framework 專案及 HOW TO:手動設定 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);
}
}