Beispiele für die methodenbasierte Abfragesyntax: Partitionierung (LINQ to DataSet)
Aktualisiert: November 2007
Die Beispiele in diesem Thema zeigen, wie Sie mit der Skip<TSource>-Methode, der SkipWhile-Methode, der Take<TSource>-Methode und der TakeWhile-Methode und mithilfe der Abfrageausdruckssyntax ein DataSet abfragen können.
Informationen zur FillDataSet-Methode, die in diesen Beispielen verwendet wird, finden Sie unter Laden von Daten in ein 'DataSet'.
In den Beispielen in diesem Thema wird auf die Tabellen Contact, Address, Product, SalesOrderHeader und SalesOrderDetail in der AdventureWorks-Beispieldatenbank zurückgegriffen.
Die Beispiele in diesem Thema beziehen sich auf die folgenden using/Imports-Anweisungen:
Option Explicit On
Imports System
Imports System.Linq
Imports System.Linq.Expressions
Imports System.Collections.Generic
Imports System.Data
Imports System.Data.SqlClient
Imports System.Data.Common
Imports System.Globalization
using System;
using System.Linq;
using System.Linq.Expressions;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Data.Common;
using System.Globalization;
Weitere Informationen dazu finden Sie unter Gewusst wie: Erstellen eines LINQ to DataSet-Projekts in Visual Studio.
Skip
Beispiel
In diesem Beispiel wird die Skip<TSource>-Methode verwendet, um, mit Ausnahme der ersten fünf Kontakte, alle Kontakte in der Contact-Tabelle abzurufen.
' Fill the DataSet.
Dim ds As New DataSet()
ds.Locale = CultureInfo.InvariantCulture
' See the FillDataSet method in the Loading Data Into a DataSet topic.
FillDataSet(ds)
Dim contacts As DataTable = ds.Tables("Contact")
Dim allButFirst5Contacts = contacts.AsEnumerable().Skip(5)
Console.WriteLine("All but first 5 contacts:")
For Each contact In allButFirst5Contacts
Console.Write("FirstName = {0} ", contact.Field(Of String)("FirstName"))
Console.WriteLine(vbTab & " LastName = " & contact.Field(Of String)("LastName"))
Next
// Fill the DataSet.
DataSet ds = new DataSet();
ds.Locale = CultureInfo.InvariantCulture;
FillDataSet(ds);
DataTable contacts = ds.Tables["Contact"];
IEnumerable<DataRow> allButFirst5Contacts = contacts.AsEnumerable().Skip(5);
Console.WriteLine("All but first 5 contacts:");
foreach (DataRow contact in allButFirst5Contacts)
{
Console.WriteLine("FirstName = {0} \tLastname = {1}",
contact.Field<string>("FirstName"),
contact.Field<string>("Lastname"));
}
Beispiel
In diesem Beispiel wird die Skip<TSource>-Methode verwendet, um, mit Ausnahme der ersten beiden Adressen, alle Adressen in Seattle abzurufen.
' Fill the DataSet.
Dim ds As New DataSet()
ds.Locale = CultureInfo.InvariantCulture
' See the FillDataSet method in the Loading Data Into a DataSet topic.
FillDataSet(ds)
Dim addresses As DataTable = ds.Tables("Address")
Dim orders As DataTable = ds.Tables("SalesOrderHeader")
Dim query = ( _
From address In addresses.AsEnumerable() _
From order In orders.AsEnumerable() _
Where (address.Field(Of Integer)("AddressID") = _
order.Field(Of Integer)("BillToAddressID")) _
And address.Field(Of String)("City") = "Seattle" _
Select New With _
{ _
.City = address.Field(Of String)("City"), _
.OrderID = order.Field(Of Integer)("SalesOrderID"), _
.OrderDate = order.Field(Of DateTime)("OrderDate") _
}).Skip(2)
Console.WriteLine("All but first 2 orders in Seattle:")
For Each addOrder In query
Console.Write("City: " & addOrder.City)
Console.Write(" Order ID: " & addOrder.OrderID)
Console.WriteLine(" Order date: " & addOrder.OrderDate)
Next
// Fill the DataSet.
DataSet ds = new DataSet();
ds.Locale = CultureInfo.InvariantCulture;
FillDataSet(ds);
DataTable addresses = ds.Tables["Address"];
DataTable orders = ds.Tables["SalesOrderHeader"];
var query = (
from address in addresses.AsEnumerable()
from order in orders.AsEnumerable()
where address.Field<int>("AddressID") == order.Field<int>("BillToAddressID")
&& address.Field<string>("City") == "Seattle"
select new
{
City = address.Field<string>("City"),
OrderID = order.Field<int>("SalesOrderID"),
OrderDate = order.Field<DateTime>("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);
}
SkipWhile
Beispiel
In diesem Beispiel werden die Methoden OrderBy und SkipWhile verwendet, um aus der Product-Tabelle alle Produkte abzurufen, deren Listenpreis größer als 300.00 ist.
' Fill the DataSet.
Dim ds As New DataSet()
ds.Locale = CultureInfo.InvariantCulture
' See the FillDataSet method in the Loading Data Into a DataSet topic.
FillDataSet(ds)
Dim products As DataTable = ds.Tables("Product")
Dim skipWhilePriceLessThan300 As IEnumerable(Of DataRow) = _
products.AsEnumerable() _
.OrderBy(Function(listprice) listprice.Field(Of Decimal)("ListPrice")) _
.SkipWhile(Function(product) product.Field(Of Decimal)("ListPrice") < 300D)
Console.WriteLine("First ListPrice less than 300.00:")
For Each product As DataRow In skipWhilePriceLessThan300
Console.WriteLine(product.Field(Of Decimal)("ListPrice"))
Next
// Fill the DataSet.
DataSet ds = new DataSet();
ds.Locale = CultureInfo.InvariantCulture;
FillDataSet(ds);
DataTable products = ds.Tables["Product"];
IEnumerable<DataRow> skipWhilePriceLessThan300 =
products.AsEnumerable()
.OrderBy(listprice => listprice.Field<decimal>("ListPrice"))
.SkipWhile(product => product.Field<decimal>("ListPrice") < 300.00M);
Console.WriteLine("Skip while ListPrice is less than 300.00:");
foreach (DataRow product in skipWhilePriceLessThan300)
{
Console.WriteLine(product.Field<decimal>("ListPrice"));
}
Take
Beispiel
In diesem Beispiel wird die Take<TSource>-Methode verwendet, um nur die ersten fünf Kontakte aus der Contact-Tabelle abzurufen.
' Fill the DataSet.
Dim ds As New DataSet()
ds.Locale = CultureInfo.InvariantCulture
' See the FillDataSet method in the Loading Data Into a DataSet topic.
FillDataSet(ds)
Dim contacts As DataTable = ds.Tables("Contact")
Dim first5Contacts = contacts.AsEnumerable().Take(5)
Console.WriteLine("First 5 contacts:")
For Each contact In first5Contacts
Console.Write("Title = " & contact.Field(Of String)("Title"))
Console.Write(vbTab & "FirstName = " & contact.Field(Of String)("FirstName"))
Console.WriteLine(vbTab & "LastName = " & contact.Field(Of String)("LastName"))
Next
// Fill the DataSet.
DataSet ds = new DataSet();
ds.Locale = CultureInfo.InvariantCulture;
FillDataSet(ds);
DataTable contacts = ds.Tables["Contact"];
IEnumerable<DataRow> first5Contacts = contacts.AsEnumerable().Take(5);
Console.WriteLine("First 5 contacts:");
foreach (DataRow contact in first5Contacts)
{
Console.WriteLine("Title = {0} \t FirstName = {1} \t Lastname = {2}",
contact.Field<string>("Title"),
contact.Field<string>("FirstName"),
contact.Field<string>("Lastname"));
}
Beispiel
In diesem Beispiel wird die Take<TSource>-Methode verwendet, um die ersten drei Adressen in Seattle abzurufen.
' Fill the DataSet.
Dim ds As New DataSet()
ds.Locale = CultureInfo.InvariantCulture
' See the FillDataSet method in the Loading Data Into a DataSet topic.
FillDataSet(ds)
Dim addresses As DataTable = ds.Tables("Address")
Dim orders As DataTable = ds.Tables("SalesOrderHeader")
Dim query = ( _
From address In addresses.AsEnumerable() _
From order In orders.AsEnumerable() _
Where (address.Field(Of Integer)("AddressID") = _
order.Field(Of Integer)("BillToAddressID")) _
And address.Field(Of String)("City") = "Seattle" _
Select New With _
{ _
.City = address.Field(Of String)("City"), _
.OrderID = order.Field(Of Integer)("SalesOrderID"), _
.OrderDate = order.Field(Of DateTime)("OrderDate") _
}).Take(3)
Console.WriteLine("First 3 orders in Seattle:")
For Each order In query
Console.Write("City: " & order.City)
Console.Write(" Order ID: " & order.OrderID)
Console.WriteLine(" Order date: " & order.OrderDate)
Next
// Fill the DataSet.
DataSet ds = new DataSet();
ds.Locale = CultureInfo.InvariantCulture;
FillDataSet(ds);
DataTable addresses = ds.Tables["Address"];
DataTable orders = ds.Tables["SalesOrderHeader"];
var query = (
from address in addresses.AsEnumerable()
from order in orders.AsEnumerable()
where address.Field<int>("AddressID") == order.Field<int>("BillToAddressID")
&& address.Field<string>("City") == "Seattle"
select new
{
City = address.Field<string>("City"),
OrderID = order.Field<int>("SalesOrderID"),
OrderDate = order.Field<DateTime>("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);
}
TakeWhile
Beispiel
In diesem Beispiel werden die Methoden OrderBy und TakeWhile verwendet, um aus der Product-Tabelle alle Produkte abzurufen, deren Listenpreis kleiner als 300.00 ist.
' Fill the DataSet.
Dim ds As New DataSet()
ds.Locale = CultureInfo.InvariantCulture
' See the FillDataSet method in the Loading Data Into a DataSet topic.
FillDataSet(ds)
Dim products As DataTable = ds.Tables("Product")
Dim takeWhileListPriceLessThan300 As IEnumerable(Of DataRow) = _
products.AsEnumerable() _
.OrderBy(Function(listprice) listprice.Field(Of Decimal)("ListPrice")) _
.TakeWhile(Function(product) product.Field(Of Decimal)("ListPrice") < 300D)
Console.WriteLine("First ListPrice less than 300.00:")
For Each product As DataRow In takeWhileListPriceLessThan300
Console.WriteLine(product.Field(Of Decimal)("ListPrice"))
Next
// Fill the DataSet.
DataSet ds = new DataSet();
ds.Locale = CultureInfo.InvariantCulture;
FillDataSet(ds);
DataTable products = ds.Tables["Product"];
IEnumerable<DataRow> takeWhileListPriceLessThan300 =
products.AsEnumerable()
.OrderBy(listprice => listprice.Field<decimal>("ListPrice"))
.TakeWhile(product => product.Field<decimal>("ListPrice") < 300.00M);
Console.WriteLine("First ListPrice less than 300:");
foreach (DataRow product in takeWhileListPriceLessThan300)
{
Console.WriteLine(product.Field<decimal>("ListPrice"));
}
Siehe auch
Konzepte
Laden von Daten in ein 'DataSet'
Übersicht über Standardabfrageoperatoren