Esempi di operatori specifici di dataset (LINQ to DataSet)DataSet-Specific Operator Examples (LINQ to DataSet)

Negli esempi di questo argomento viene illustrato l'uso del metodo CopyToDataTable e della classe DataRowComparer.The examples in this topic demonstrate how to use the CopyToDataTable method and the DataRowComparer class.

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.

CopyToDataTableCopyToDataTable

EsempioExample

In questo esempio viene caricato un oggetto DataTable con i risultati della query usando il metodo CopyToDataTable.This example loads a DataTable with query results by using the CopyToDataTable method.

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

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

IEnumerable<DataRow> query =
    from contact in contacts1.AsEnumerable()
    where contact.Field<string>("Title") == "Ms."
        && contact.Field<string>("FirstName") == "Carla"
    select contact;

DataTable contacts2 = query.CopyToDataTable();

foreach (DataRow contact in contacts2.AsEnumerable())
{
    Console.WriteLine("ID:{0} Name: {1}, {2}",
        contact.Field<Int32>("ContactID"),
        contact.Field<string>("LastName"),
        contact.Field<string>("FirstName"));
}
' 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 contactTable As DataTable = ds.Tables("Contact")

Dim query = _
    From contact In contactTable.AsEnumerable() _
    Where contact.Field(Of String)("Title") = "Ms." _
            And contact.Field(Of String)("FirstName") = "Carla" _
    Select contact

Dim contacts = query.CopyToDataTable().AsEnumerable()

For Each contact In contacts
    Console.Write("ID: " & contact.Field(Of Integer)("ContactID"))
    Console.WriteLine(" Name: " & contact.Field(Of String)("LastName") & _
                      ", " & contact.Field(Of String)("FirstName"))
Next

DataRowComparerDataRowComparer

EsempioExample

In questo esempio vengono confrontate due righe di dati diverse usando DataRowComparer.This example compares two different data rows by using DataRowComparer.

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

// Get two rows from the SalesOrderHeader table.
DataTable table = ds.Tables["SalesOrderHeader"];
DataRow left = (DataRow)table.Rows[0];
DataRow right = (DataRow)table.Rows[1];

// Compare the two different rows.
IEqualityComparer<DataRow> comparer = DataRowComparer.Default;

bool bEqual = comparer.Equals(left, right);
if (bEqual)
    Console.WriteLine("The two rows are equal");
else
    Console.WriteLine("The two rows are not equal");

// Get the hash codes of the two rows.
Console.WriteLine("The hashcodes for the two rows are {0}, {1}",
    comparer.GetHashCode(left),
    comparer.GetHashCode(right));

Vedere ancheSee Also

Caricamento di dati in un oggetto DataSetLoading Data Into a DataSet
Esempi di LINQ to DataSetLINQ to DataSet Examples