Esempi di sintassi di query basate sul metodo: conversioneMethod-Based Query Syntax Examples: Conversion

Negli esempi in questo argomento viene illustrato come utilizzare il ToArray, ToDictionary e ToList metodi per eseguire una query di modello Sales di AdventureWorks utilizzando la sintassi di query basate su metodo.The examples in this topic demonstrate how to use the ToArray, ToDictionary and ToList methods to query the AdventureWorks Sales Model using method-based query syntax. Il modello Sales di AdventureWorks usato in questi esempi è compilato in base alle tabelle Contact, Address, Product, SalesOrderHeader e SalesOrderDetail del database di esempio AdventureWorks.The AdventureWorks Sales Model used in these examples is built from 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.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;
Option Explicit On
Option Strict On
Imports System.Data.Objects
Imports System.Globalization

ToArrayToArray

EsempioExample

Nell'esempio seguente viene usato il metodo ToArray per restituire immediatamente una matrice da una sequenza.The following example uses the ToArray method to immediately evaluate a sequence into an array.

using (AdventureWorksEntities context = new AdventureWorksEntities())
{
    ObjectSet<Product> products = context.Products;

    Product[] prodArray = (
        from product in products
        orderby product.ListPrice descending
        select product).ToArray();

    Console.WriteLine("Every price from highest to lowest:");
    foreach (Product product in prodArray)
    {
        Console.WriteLine(product.ListPrice);
    }
}
Using context As New AdventureWorksEntities
    Dim products As ObjectSet(Of Product) = context.Products

    Dim prodArray As Product() = ( _
        From product In products _
        Order By product.ListPrice Descending _
        Select product).ToArray()

    Console.WriteLine("The list price from highest to lowest:")
    For Each prod As Product In prodArray
        Console.WriteLine(prod.ListPrice)
    Next
End Using

ToDictionaryToDictionary

EsempioExample

Nell'esempio seguente viene usato il metodo ToDictionary per restituire immediatamente un dizionario da una sequenza e un'espressione chiave correlata.The following example uses the ToDictionary method to immediately evaluate a sequence and a related key expression into a dictionary.

using (AdventureWorksEntities context = new AdventureWorksEntities())
{
    ObjectSet<Product> products = context.Products;

    Dictionary<String, Product> scoreRecordsDict = products.
            ToDictionary(record => record.Name);

    Console.WriteLine("Top Tube's ProductID: {0}",
            scoreRecordsDict["Top Tube"].ProductID);
}
Using context As New AdventureWorksEntities
    Dim products As ObjectSet(Of Product) = context.Products

    Dim scoreRecordsDict As Dictionary(Of String, Product) = _
        products.ToDictionary(Function(record) record.Name)

    Console.WriteLine("Top Tube's ProductID: {0}", _
        scoreRecordsDict("Top Tube").ProductID)
End Using

ToListToList

EsempioExample

Nell'esempio seguente viene usato il metodo ToList per restituire immediatamente un oggetto List<T>, dove T è di tipo DataRow, da una sequenza.The following example uses the ToList method to immediately evaluate a sequence into a List<T>, where T is of type DataRow.

using (AdventureWorksEntities context = new AdventureWorksEntities())
{
    ObjectSet<Product> products = context.Products;

    List<Product> query =
        (from product in products
         orderby product.Name
         select product).ToList();

    Console.WriteLine("The product list, ordered by product name:");
    foreach (Product product in query)
    {
        Console.WriteLine(product.Name.ToLower(CultureInfo.InvariantCulture));
    }
}
Using context As New AdventureWorksEntities
    Dim products As ObjectSet(Of Product) = context.Products

    Dim prodList As List(Of Product) = ( _
        From product In products _
        Order By product.Name _
        Select product).ToList()

    Console.WriteLine("The product list, ordered by product name:")
    For Each prod As Product In prodList
        Console.WriteLine(prod.Name.ToLower(CultureInfo.InvariantCulture))
    Next
End Using

Vedere ancheSee Also

Query in LINQ to EntitiesQueries in LINQ to Entities