共用方式為


以方法為基礎的查詢語法範例:轉換 (LINQ to Entities)

本主題中的範例將示範如何使用 ToArrayToDictionaryToList 方法,透過以方法為基礎的查詢語法來查詢 AdventureWorks Sales Model。 這些範例中使用的 AdventureWorks Sales Model 是從 AdventureWorks 範例資料庫中的 Contact、Address、Product、SalesOrderHeader 和 SalesOrderDetail 資料表所建立。

此主題中的範例將使用下列 using/Imports 陳述式:

Option Explicit On
Option Strict On
Imports System.Data.Objects
Imports System.Globalization
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;

ToArray

範例

以下範例使用 ToArray 方法,將序列立即評估為陣列。

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

ToDictionary

範例

下列範例會使用 ToDictionary 方法,立即將序列和相關的索引鍵運算式評估為字典。

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

ToList

範例

下列範例會使用 ToList 方法,立即將序列評估為 List,其中 T 的型別為 DataRow

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

另請參閱

概念

LINQ to Entities 中的查詢