Export (0) Print
Expand All

How to: Convert the Results of a LINQ to Entities Query to an Array

[This topic is pre-release documentation and is subject to change in future releases. Blank topics are included as placeholders.]

Use the ToArray method to create an array from the results of a LINQ to Entities query. Calling ToArray also forces immediate execution of the query. For examples of converting the results of a LINQ to Entities query to a Dictionary or List, see Method-Based Query Syntax Examples: Conversion (LINQ to Entities).

Example

The following example uses the ToArray method to immediately evaluate the query and convert the sequence into an array.

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

Compiling the Code

The example in this topic contains references to objects and namespaces that are defined in the sample project in How to: Create a LINQ to Entities Project In Visual Studio. To compile and run this example, paste it into the Main method.

See Also

Show:
© 2014 Microsoft