Procédure : filtrer des données (Entity Framework)
Cette rubrique montre comment filtrer des résultats de requête. L'exemple retourne une collection d'objets SalesOrderHeader qui représente toutes les commandes en ligne d'une valeur supérieure à 2 500 $. Le même exemple est repris en utilisant les technologies de requête Entity Framework suivantes :
LINQ to Entities
Entity SQL avec ObjectQuery<T>
Méthodes du Générateur de requêtes d'ObjectQuery<T>
L'exemple de cette rubrique est basé sur le modèle de vente Adventure Works Sales Model. Pour exécuter le code de cette rubrique, vous devez déjà avoir ajouté le modèle de vente AdventureWorks à votre projet et configuré ce dernier pour qu'il utilise Entity Framework. Pour plus d'informations, consultez Procédure : utiliser l'Assistant EDM (Entity Framework) ou Procédure : configurer manuellement un projet Entity Framework et Procédure : définir manuellement un modèle EDM (Entity Data Model) (Entity Framework).
Exemple
L'exemple ci-dessous utilise 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);
}
}
L'exemple ci-dessous utilise 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);
}
}
L'exemple ci-dessous utilise la méthode du Générateur de requêtes.
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);
}
}