Voorbeelden van op methode gebaseerde querysyntaxis: Bestellen (LINQ naar DataSet)
De voorbeelden in dit onderwerp laten zien hoe u de OrderBy, Reverseen methoden gebruikt om een DataSet query uit te voeren en ThenBy de resultaten te ordenen met behulp van de syntaxis van de methodequery.
De FillDataSet
methode die in deze voorbeelden wordt gebruikt, wordt opgegeven in Gegevens laden in een DataSet.
In de voorbeelden in dit onderwerp worden de tabellen Contact, Adres, Product, SalesOrderHeader en SalesOrderDetail gebruikt in de voorbeelddatabase AdventureWorks.
In de voorbeelden in dit onderwerp worden de volgende using
/Imports
instructies gebruikt:
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Globalization;
using System.Linq;
using System.Windows.Forms;
Option Explicit On
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
Zie How to: Create a LINQ to DataSet Project in Visual Studio (Een LINQ maken naar DataSet-project in Visual Studio) voor meer informatie.
Orderby
Opmerking
In dit voorbeeld wordt de OrderBy methode gebruikt met een aangepaste vergelijkingsfunctie om een hoofdlettergevoelige soort achternamen uit te voeren.
// Fill the DataSet.
DataSet ds = new DataSet();
ds.Locale = CultureInfo.InvariantCulture;
FillDataSet(ds);
DataTable contacts = ds.Tables["Contact"];
IEnumerable<DataRow> query =
contacts.AsEnumerable().OrderBy(contact => contact.Field<string>("LastName"),
new CaseInsensitiveComparer());
foreach (DataRow contact in query)
{
Console.WriteLine(contact.Field<string>("LastName"));
}
' 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 query As IEnumerable(Of DataRow) = _
contacts.AsEnumerable().OrderBy(Function(contact) _
contact.Field(Of String)("LastName"), _
New CaseInsensitiveComparer())
For Each contact As DataRow In query
Console.WriteLine(contact.Field(Of String)("LastName"))
Next
Terugboeken
Opmerking
In dit voorbeeld wordt de methode gebruikt voor het Reverse maken van een lijst met orders die OrderDate
ouder zijn dan 20 februari 2002.
// Fill the DataSet.
DataSet ds = new DataSet();
ds.Locale = CultureInfo.InvariantCulture;
FillDataSet(ds);
DataTable orders = ds.Tables["SalesOrderHeader"];
IEnumerable<DataRow> query = (
from order in orders.AsEnumerable()
where order.Field<DateTime>("OrderDate") < new DateTime(2002, 02, 20)
select order).Reverse();
Console.WriteLine("A backwards list of orders where OrderDate < Feb 20, 2002");
foreach (DataRow order in query)
{
Console.WriteLine(order.Field<DateTime>("OrderDate"));
}
' 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 orders As DataTable = ds.Tables("SalesOrderHeader")
Dim query = ( _
From order In orders.AsEnumerable() _
Where order.Field(Of DateTime)("OrderDate") < New DateTime(2002, 2, 20) _
Select order).Reverse()
Console.WriteLine("A backwards list of orders where OrderDate < Feb 20, 2002")
For Each order In query
Console.WriteLine(order.Field(Of DateTime)("OrderDate"))
Next
ThenBy
Opmerking
In dit voorbeeld worden OrderBy en ThenBy methoden met een aangepaste vergelijkingsfunctie gebruikt om eerst te sorteren op catalogusprijs en vervolgens een niet-hoofdlettergevoelig aflopend type productnamen uit te voeren.
// Fill the DataSet.
DataSet ds = new DataSet();
ds.Locale = CultureInfo.InvariantCulture;
FillDataSet(ds);
DataTable products = ds.Tables["Product"];
IEnumerable<DataRow> query =
products.AsEnumerable().OrderBy(product => product.Field<Decimal>("ListPrice"))
.ThenBy(product => product.Field<string>("Name"),
new CaseInsensitiveComparer());
foreach (DataRow product in query)
{
Console.WriteLine("Product ID: {0} Product Name: {1} List Price {2}",
product.Field<int>("ProductID"),
product.Field<string>("Name"),
product.Field<Decimal>("ListPrice"));
}
' 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 query As IEnumerable(Of DataRow) = _
products.AsEnumerable().OrderBy(Function(product) product.Field(Of Decimal)("ListPrice")) _
.ThenBy(Function(product) product.Field(Of String)("Name"), _
New CaseInsensitiveComparer())
For Each product As DataRow In query
Console.WriteLine("Product ID: {0} Product Name: {1} List Price {2}", _
product.Field(Of Integer)("ProductID"), _
product.Field(Of String)("Name"), _
product.Field(Of Decimal)("ListPrice"))
Next