Freigeben über


Beispiele für die Abfrageausdruckssyntax: Partitionierung (LINQ to Entities)

In diesem Thema wird anhand von Beispielen gezeigt, wie Sie mithilfe der Skip-Methode, der Take-Methode und der Abfrageausdruckssyntax das AdventureWorks Sales-Modell abfragen können. Für das in den Beispielen verwendete AdventureWorks Sales-Modell wurde auf die Contact-, Address-, Product-, SalesOrderHeader- und SalesOrderDetail-Tabellen der AdventureWorks-Beispieldatenbank zurückgegriffen.

In den Beispielen in diesem Thema werden die folgenden using/Imports-Anweisungen verwendet:

Option Explicit On
Option Strict On
Imports System.Data.Objects
Imports System.Globalization
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;

Skip

Beispiel

Im folgenden Beispiel wird die Skip-Methode verwendet, um, mit Ausnahme der ersten beiden Adressen, alle Adressen in Seattle abzurufen.

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

Take

Beispiel

Im folgenden Beispiel wird die Take-Methode verwendet, um die ersten drei Adressen in Seattle abzurufen.

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

Siehe auch

Konzepte

Abfragen in LINQ to Entities