共用方式為


HOW TO:將 LINQ to Entities 查詢轉換成陣列

使用 ToArray 方法從 LINQ 到實體 查詢的結果建立查詢。呼叫 ToArray 也會強制查詢立即執行。如需將 LINQ 到實體 查詢轉換成 DictionaryList 的範例,請參閱 以方法為基礎的查詢語法範例:轉換 (LINQ to Entities)

範例

以下範例使用 ToArray 方法立即評估查詢並且將此序列轉換成陣列。

Using AWEntities As New AdventureWorksEntities
    Dim products As ObjectQuery(Of Product) = AWEntities.Product

    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 AWEntities = new AdventureWorksEntities())
{
    ObjectQuery<Product> products = AWEntities.Product;

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

編譯程式碼

本主題的範例包含了物件和命名空間的參考,這些物件和命名空間定義於 HOW TO:在 Visual Studio 中建立 LINQ to Entities 專案 中的範例專案內。若要編譯和執行此範例,請將它貼到 Main 方法中。

另請參閱

概念

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

其他資源

查詢 Entity Data Model (Entity Framework 工作)