Procédure : parcourir des résultats de requête (Entity Framework)
Cette rubrique montre comment parcourir les résultats d'une requête. L'exemple obtient cinq objets Product après avoir ignoré les trois premiers du résultat de la requête, lequel est trié par valeur Product.ListPrice. Le même exemple est repris en utilisant chacune des 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
Cet exemple utilise LINQ to Entities .
Using context As New AdventureWorksEntities
'LINQ to Entities only supports Skip on ordered collections.
Dim products As IOrderedQueryable(Of Product) = _
context.Products.OrderBy(Function(p) p.ListPrice)
Dim allButFirst3Products As IQueryable(Of Product) = products.Skip(3)
Console.WriteLine("All but first 3 products:")
For Each product As Product In allButFirst3Products
Console.WriteLine("Name: {0} \t ID: {1}", _
product.Name, _
product.ProductID)
Next
End Using
using (AdventureWorksEntities context = new AdventureWorksEntities())
{
// LINQ to Entities only supports Skip on ordered collections.
IOrderedQueryable<Product> products = context.Products
.OrderBy(p => p.ListPrice);
IQueryable<Product> allButFirst3Products = products.Skip(3);
Console.WriteLine("All but first 3 products:");
foreach (Product product in allButFirst3Products)
{
Console.WriteLine("Name: {0} \t ID: {1}",
product.Name,
product.ProductID);
}
}
Cet exemple utilise Entity SQL .
Using context As New AdventureWorksEntities()
' Create a query that takes two parameters.
Dim queryString As String = "SELECT VALUE product FROM AdventureWorksEntities.Products AS product " & _
" order by product.ListPrice SKIP @skip LIMIT @limit"
Dim productQuery As New ObjectQuery(Of Product)(queryString, context)
' Add parameters to the collection.
productQuery.Parameters.Add(New ObjectParameter("skip", 3))
productQuery.Parameters.Add(New ObjectParameter("limit", 5))
' Iterate through the collection of Contact items.
For Each result As Product In productQuery
Console.WriteLine("ID: {0}; Name: {1}", result.ProductID, result.Name)
Next
End Using
using (AdventureWorksEntities context =
new AdventureWorksEntities())
{
// Create a query that takes two parameters.
string queryString =
@"SELECT VALUE product FROM
AdventureWorksEntities.Products AS product
order by product.ListPrice SKIP @skip LIMIT @limit";
ObjectQuery<Product> productQuery =
new ObjectQuery<Product>(queryString, context);
// Add parameters to the collection.
productQuery.Parameters.Add(new ObjectParameter("skip", 3));
productQuery.Parameters.Add(new ObjectParameter("limit", 5));
// Iterate through the collection of Contact items.
foreach (Product result in productQuery)
Console.WriteLine("ID: {0}; Name: {1}",
result.ProductID, result.Name);
}
Cet exemple utilise la méthode du Générateur de requêtes.
Using context As New AdventureWorksEntities()
' Define the parameters used to define the "page" of returned data.
Dim skipValue As Integer = 3
Dim limitValue As Integer = 5
' Define a query that returns a "page" or the full
' Product data using the Skip and Top methods.
' When Top() follows Skip(), it acts like the LIMIT statement.
Dim query As ObjectQuery(Of Product) = _
context.Products.Skip("it.ListPrice", "@skip", _
New ObjectParameter("skip", skipValue)).Top("@limit", New ObjectParameter("limit", limitValue))
' Iterate through the page of Product items.
For Each result As Product In query
Console.WriteLine("ID: {0}; Name: {1}", result.ProductID, result.Name)
Next
End Using
using (AdventureWorksEntities context =
new AdventureWorksEntities())
{
// Define the parameters used to define the "page" of returned data.
int skipValue = 3;
int limitValue = 5;
// Define a query that returns a "page" or the full
// Product data using the Skip and Top methods.
// When Top() follows Skip(), it acts like the LIMIT statement.
ObjectQuery<Product> query = context.Products
.Skip("it.ListPrice", "@skip",
new ObjectParameter("skip", skipValue))
.Top("@limit", new ObjectParameter("limit", limitValue));
// Iterate through the page of Product items.
foreach (Product result in query)
Console.WriteLine("ID: {0}; Name: {1}",
result.ProductID, result.Name);
}
Voir aussi
Référence
SKIP (Entity SQL)
LIMIT (Entity SQL)
Skip
Top