Esempi di sintassi di espressione di query: operatori di elemento (LINQ to DataSet)Query Expression Syntax Examples: Element Operators (LINQ to DataSet)

Negli esempi di questo argomento viene illustrato come usare i metodi First e ElementAt per ottenere elementi di DataRow da DataSet usando la sintassi delle espressioni di query.The examples in this topic demonstrate how to use the First and ElementAt methods to get DataRow elements from a DataSet using the query expression syntax.

Il FillDataSet metodo usato in questi esempi è specificato nel durante il caricamento dei dati in un set di dati.The FillDataSet method used in these examples is specified in Loading Data Into a DataSet.

Negli esempi di questo argomento vengono usate le tabelle Contact, Address, Product, SalesOrderHeader e SalesOrderDetail del database di esempio AdventureWorks.The examples in this topic use the Contact, Address, Product, SalesOrderHeader, and SalesOrderDetail tables in the AdventureWorks sample database.

Gli esempi in questo argomento usano seguenti using / Imports istruzioni:The examples in this topic use the following using/Imports statements:

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

Per ulteriori informazioni, vedere procedura: creare un LINQ to DataSet progetto In Visual Studio.For more information, see How to: Create a LINQ to DataSet Project In Visual Studio.

ElementAtElementAt

EsempioExample

In questo esempio viene usato il metodo ElementAt per recuperare il quinto indirizzo in cui PostalCode == "M4B 1V7".This example uses the ElementAt method to retrieve the fifth address where PostalCode == "M4B 1V7".

// Fill the DataSet.
DataSet ds = new DataSet();
ds.Locale = CultureInfo.InvariantCulture;
FillDataSet(ds);

DataTable addresses = ds.Tables["Address"];

var fifthAddress = (
    from address in addresses.AsEnumerable()
    where address.Field<string>("PostalCode") == "M4B 1V7"
    select address.Field<string>("AddressLine1"))
.ElementAt(5);

Console.WriteLine("Fifth address where PostalCode = 'M4B 1V7': {0}",
    fifthAddress);
' 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 fifthAddress = ( _
    From address In addresses.AsEnumerable() _
    Where address.Field(Of String)("PostalCode") = "M4B 1V7" _
    Select address.Field(Of String)("AddressLine1")).ElementAt(5)

Console.WriteLine("Fifth address where PostalCode = 'M4B 1V7': " & _
        fifthAddress)

FirstFirst

EsempioExample

In questo esempio viene usato il metodo First per restituire il primo contatto il cui nome è 'Brooke'.This example uses the First method to return the first contact whose first name is 'Brooke'.

// Fill the DataSet.
DataSet ds = new DataSet();
ds.Locale = CultureInfo.InvariantCulture;
FillDataSet(ds);

DataTable contacts = ds.Tables["Contact"];

DataRow query = (
    from contact in contacts.AsEnumerable()
    where (string)contact["FirstName"] == "Brooke"
    select contact)
    .First();

Console.WriteLine("ContactID: " + query.Field<int>("ContactID"));
Console.WriteLine("FirstName: " + query.Field<string>("FirstName"));
Console.WriteLine("LastName: " + query.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 = ( _
    From contact In contacts.AsEnumerable() _
    Where contact.Field(Of String)("FirstName") = "Brooke" _
    Select contact).First()

Console.WriteLine("ContactID: " & query.Field(Of Integer)("ContactID"))
Console.WriteLine("FirstName: " & query.Field(Of String)("FirstName"))
Console.WriteLine("LastName: " & query.Field(Of String)("LastName"))

Vedere ancheSee Also

Caricamento di dati in un oggetto DataSetLoading Data Into a DataSet
Esempi di LINQ to DataSetLINQ to DataSet Examples
Cenni preliminari sugli operatori di query standardStandard Query Operators Overview