Condividi tramite


Procedura: filtrare i dati (Entity Framework)

In questo argomento viene illustrato come filtrare i risultati di una query. Nell'esempio viene restituito un insieme di oggetti SalesOrderHeader che rappresentano tutti gli ordini effettuati in linea con un valore superiore a 2.500 dollari. Lo stesso esempio viene illustrato utilizzando le tecnologie di query Entity Framework seguenti:

  • LINQ to Entities

  • Entity SQL con ObjectQuery<T>

  • Metodi del generatore di query di ObjectQuery<T>

Gli esempi inclusi in questo argomento sono basati sul modello Sales di AdventureWorks. Per eseguire il codice incluso in questo esempio, è necessario avere già aggiunto il modello Sales di AdventureWorks al progetto e avere configurato il progetto per l'utilizzo di Entity Framework. A tale scopo, completare le procedure descritte in Procedura: configurare manualmente un progetto di Entity Framework e Procedura: definire manualmente un modello EDM (Entity Framework). È inoltre possibile utilizzare la procedura guidata Entity Data Model per definire il modello Sales di AdventureWorks. Per ulteriori informazioni, vedere Procedura: utilizzare la procedura guidata Entity Data Model (Entity Framework).

Esempio

Di seguito viene fornito un esempio di utilizzo di LINQ to Entities.

Using context As AdventureWorksEntities = _
New AdventureWorksEntities()

    ' Specify the order amount.
    Dim orderCost As Integer = 2500

    Try
        ' Define a LINQ query that returns only online orders
        ' more than the specified amount.
        Dim onlineOrders = _
        From order As SalesOrderHeader In context.SalesOrderHeader() _
            Where order.OnlineOrderFlag = True And _
            order.TotalDue > orderCost _
            Select order

        ' Print order information.
        For Each onlineOrder In onlineOrders

            Console.WriteLine("Order ID:{0}Order date: " _
                + "{1}:d Order number: {2}", _
                onlineOrder.SalesOrderID, _
                onlineOrder.OrderDate, _
                onlineOrder.SalesOrderNumber)
        Next
    Catch ex As EntitySqlException
        Console.WriteLine(ex.ToString())
    End Try
End Using
using (AdventureWorksEntities context = new AdventureWorksEntities())
{
    // Specify the order amount.
    int orderCost = 2500;

    try
    {
        // Define a LINQ query that returns only online orders
        // more than the specified amount.
        var onlineOrders =
            from order in context.SalesOrderHeader
            where order.OnlineOrderFlag == true &&
            order.TotalDue > orderCost
            select order;

        // Print order information.
        foreach (var onlineOrder in onlineOrders)
        {
            Console.WriteLine("Order ID: {0} Order date: "
                + "{1:d} Order number: {2}",
                onlineOrder.SalesOrderID,
                onlineOrder.OrderDate,
                onlineOrder.SalesOrderNumber);
        }
    }
    catch (EntitySqlException ex)
    {
        Console.WriteLine(ex.ToString());
    }
}

Di seguito viene fornito un esempio di utilizzo di Entity SQL.

Using context As AdventureWorksEntities = _
New AdventureWorksEntities()

    ' Specify the order amount.
    Dim orderCost As Integer = 2500

    ' Specify the Entity SQL query that returns only online orders
    ' more than the specified amount.
    Dim queryString As String = "SELECT VALUE o FROM SalesOrderHeader AS o " _
    + "WHERE o.OnlineOrderFlag = TRUE AND o.TotalDue > @ordercost"

    Try
        ' Define an ObjectQuery and pass the maxOrderCost parameter.
        Dim onlineOrders As ObjectQuery(Of SalesOrderHeader) = _
            New ObjectQuery(Of SalesOrderHeader)(queryString, context)
        onlineOrders.Parameters.Add( _
            New ObjectParameter("ordercost", orderCost))

        ' Print order information.
        For Each onlineOrder In onlineOrders
            Console.WriteLine("Order ID:{0}Order date: " _
                + "{1}:d Order number: {2}", _
                onlineOrder.SalesOrderID, _
                onlineOrder.OrderDate, _
                onlineOrder.SalesOrderNumber)
        Next
    Catch ex As EntitySqlException
        Console.WriteLine(ex.ToString())
    End Try
End Using
using (AdventureWorksEntities context = new AdventureWorksEntities())
{
    // Specify the order amount.
    decimal orderCost = 2500;

    // Specify the Entity SQL query that returns only online orders
    // more than the specified amount.
    string queryString = @"SELECT VALUE o FROM SalesOrderHeader AS o 
        WHERE o.OnlineOrderFlag = TRUE AND o.TotalDue > @ordercost";

    try
    {
        // Define an ObjectQuery and pass the maxOrderCost parameter.
        ObjectQuery<SalesOrderHeader> onlineOrders =
            new ObjectQuery<SalesOrderHeader>(queryString, context);
        onlineOrders.Parameters.Add(
            new ObjectParameter("ordercost", orderCost));

        // Print order information.
        foreach (var onlineOrder in onlineOrders)
        {
            Console.WriteLine("Order ID: {0} Order date: "
                + "{1:d} Order number: {2}",
                onlineOrder.SalesOrderID,
                onlineOrder.OrderDate,
                onlineOrder.SalesOrderNumber);
        }
    }
    catch (EntitySqlException ex)
    {
        Console.WriteLine(ex.ToString());
    }
}

Di seguito viene fornito un esempio del metodo del generatore di query.

Using context As AdventureWorksEntities = _
New AdventureWorksEntities()

    ' Specify the order amount.
    Dim orderCost As Integer = 2500

    Try

        ' Define an ObjectQuery that returns only online orders 
        ' more than the specified amount.
        Dim onlineOrders As ObjectQuery(Of SalesOrderHeader) = _
        context.SalesOrderHeader.Where("it.OnlineOrderFlag = TRUE") _
            .Where("it.TotalDue > @ordercost", _
            New ObjectParameter("ordercost", orderCost))

        ' Print order information.
        For Each onlineOrder In onlineOrders
            Console.WriteLine("Order ID:{0}Order date: " _
                + "{1}:d Order number: {2}", _
                onlineOrder.SalesOrderID, _
                onlineOrder.OrderDate, _
                onlineOrder.SalesOrderNumber)
        Next
    Catch ex As EntitySqlException
        Console.WriteLine(ex.ToString())
    End Try
End Using
using (AdventureWorksEntities context = new AdventureWorksEntities())
{
    // Specify the order amount.
    int orderCost = 2500;

   try
    {
        // Define an ObjectQuery that returns only online orders 
        // more than the specified amount.
        ObjectQuery<SalesOrderHeader> onlineOrders =
            context.SalesOrderHeader
            .Where("it.OnlineOrderFlag = TRUE AND it.TotalDue > @ordercost",
            new ObjectParameter("ordercost", orderCost));

        // Print order information.
        foreach (var onlineOrder in onlineOrders)
        {
            var items = (from item in context.SalesOrderDetail
                        where item.SalesOrderHeader == onlineOrder
                        select item).Take(3);
             Console.WriteLine("Order ID: {0} Order date: "
                + "{1:d} Order number: {2}",
                onlineOrder.SalesOrderID,
                onlineOrder.OrderDate,
                onlineOrder.SalesOrderNumber);
        }
    }
    catch (EntitySqlException ex)
    {
        Console.WriteLine(ex.ToString());
    }
}

Vedere anche

Altre risorse

Esecuzione di query su Entity Data Model (attività di Entity Framework)