Share via


Voorbeelden van querysyntaxis op basis van methoden: partitioneren

De voorbeelden in dit onderwerp laten zien hoe u de Skipen Take methoden gebruikt om query's uit te voeren op het AdventureWorks Sales Model met behulp van de syntaxis van query-expressies. Het AdventureWorks Sales Model dat in deze voorbeelden wordt gebruikt, is gebaseerd op de tabellen Contact, Adres, Product, SalesOrderHeader en SalesOrderDetail in de voorbeelddatabase AdventureWorks.

In de voorbeelden in dit onderwerp worden de volgende using/Imports instructies gebruikt:

using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Objects;
using System.Globalization;
using System.Data.EntityClient;
using System.Data.SqlClient;
using System.Data.Common;

Option Explicit On
Option Strict On
Imports System.Data.Objects
Imports System.Globalization

Overslaan

Opmerking

In het volgende voorbeeld wordt de Skip methode gebruikt om alle behalve de eerste drie contactpersonen van de tabel Contactpersoon op te halen.

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);
    }
}
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

Opmerking

In het volgende voorbeeld wordt de Skip methode gebruikt om alle behalve de eerste twee adressen in Seattle op te halen.

using (AdventureWorksEntities context = new AdventureWorksEntities())
{
    ObjectSet<Address> addresses = context.Addresses;
    ObjectSet<SalesOrderHeader> orders = context.SalesOrderHeaders;

    //LINQ to Entities only supports Skip on ordered collections.
    var query = (
        from address in addresses
        from order in orders
        where address.AddressID == order.Address.AddressID
             && address.City == "Seattle"
        orderby order.SalesOrderID
        select new
        {
            City = address.City,
            OrderID = order.SalesOrderID,
            OrderDate = order.OrderDate
        }).Skip(2);

    Console.WriteLine("All but first 2 orders in Seattle:");
    foreach (var order in query)
    {
        Console.WriteLine("City: {0} Order ID: {1} Total Due: {2:d}",
            order.City, order.OrderID, order.OrderDate);
    }
Using context As New AdventureWorksEntities
    Dim orders As ObjectSet(Of SalesOrderHeader) = context.SalesOrderHeaders
    Dim addresses As ObjectSet(Of Address) = context.Addresses

    'LINQ to Entities only supports Skip on ordered collections.
    Dim query = ( _
        From address In addresses _
        From order In orders _
        Where address.AddressID = order.Address.AddressID _
                 And address.City = "Seattle" _
        Order By order.SalesOrderID _
        Select New With _
        { _
            .City = address.City, _
            .OrderID = order.SalesOrderID, _
            .OrderDate = order.OrderDate _
        }).Skip(2)

    Console.WriteLine("All but first 2 orders in Seattle:")
    For Each order In query
        Console.WriteLine("City: {0} Order ID: {1} Total Due: {2:d}", _
            order.City, order.OrderID, order.OrderDate)
    Next
End Using

Take

Opmerking

In het volgende voorbeeld wordt de Take methode gebruikt om alleen de eerste vijf contactpersonen uit de tabel Contactpersoon op te halen.

using (AdventureWorksEntities context = new AdventureWorksEntities())
{
    IQueryable<Contact> first5Contacts = context.Contacts.Take(5);

    Console.WriteLine("First 5 contacts:");
    foreach (Contact contact in first5Contacts)
    {
        Console.WriteLine("Title = {0} \t FirstName = {1} \t Lastname = {2}",
            contact.Title,
            contact.FirstName,
            contact.LastName);
    }
}
Using context As New AdventureWorksEntities
    Dim contacts As ObjectSet(Of Contact) = context.Contacts

    Dim first5Contacts As IQueryable(Of Contact) = contacts.Take(5)

    Console.WriteLine("First 5 contacts:")
    For Each contact As Contact In first5Contacts
        Console.WriteLine("Title = {0} " & vbTab & " FirstName = {1} " _
            & vbTab & " Lastname = {2}", contact.Title, contact.FirstName, _
            contact.LastName)
    Next
End Using

Opmerking

In het volgende voorbeeld wordt de Take methode gebruikt om de eerste drie adressen op te halen in Seattle.

String city = "Seattle";
using (AdventureWorksEntities context = new AdventureWorksEntities())
{
    ObjectSet<Address> addresses = context.Addresses;
    ObjectSet<SalesOrderHeader> orders = context.SalesOrderHeaders;

    var query = (
        from address in addresses
        from order in orders
        where address.AddressID == order.Address.AddressID
             && address.City == city
        select new
        {
            City = address.City,
            OrderID = order.SalesOrderID,
            OrderDate = order.OrderDate
        }).Take(3);
    Console.WriteLine("First 3 orders in Seattle:");
    foreach (var order in query)
    {
        Console.WriteLine("City: {0} Order ID: {1} Total Due: {2:d}",
            order.City, order.OrderID, order.OrderDate);
    }
}
Dim city = "Seattle"
Using context As New AdventureWorksEntities
    Dim orders As ObjectSet(Of SalesOrderHeader) = context.SalesOrderHeaders
    Dim addresses As ObjectSet(Of Address) = context.Addresses

    Dim query = ( _
        From address In addresses _
        From order In orders _
        Where address.AddressID = order.Address.AddressID _
                 And address.City = city _
        Select New With _
        { _
            .City = address.City, _
            .OrderID = order.SalesOrderID, _
            .OrderDate = order.OrderDate _
        }).Take(3)

    Console.WriteLine("First 3 orders in Seattle:")
    For Each order In query
        Console.WriteLine("City: {0} Order ID: {1} Total Due: {2:d}", _
            order.City, order.OrderID, order.OrderDate)
    Next
End Using

Zie ook