ObjectQuery<T>.Intersect(ObjectQuery<T>) ObjectQuery<T>.Intersect(ObjectQuery<T>) ObjectQuery<T>.Intersect(ObjectQuery<T>) ObjectQuery<T>.Intersect(ObjectQuery<T>) Method

Definizione

Limita i risultati della query includendo solo i risultati presenti in un'altra query di oggetto.Limits the query results by including only the results that exist in another object query.

public:
 System::Data::Objects::ObjectQuery<T> ^ Intersect(System::Data::Objects::ObjectQuery<T> ^ query);
public System.Data.Objects.ObjectQuery<T> Intersect (System.Data.Objects.ObjectQuery<T> query);
member this.Intersect : System.Data.Objects.ObjectQuery<'T> -> System.Data.Objects.ObjectQuery<'T>
Public Function Intersect (query As ObjectQuery(Of T)) As ObjectQuery(Of T)

Parametri

query
ObjectQuery<T> ObjectQuery<T> ObjectQuery<T> ObjectQuery<T>

Oggetto ObjectQuery<T> che rappresenta i risultati da includere nella query.An ObjectQuery<T> that represents the results to include in the query.

Restituisce

Nuova istanza di ObjectQuery<T> equivalente all'istanza originale con l'operatore INTERSECT applicato in base al parametro query specificato.A new ObjectQuery<T> instance that is equivalent to the original instance with INTERSECT applied based on the specified query.

Eccezioni

Il parametro query è null oppure una stringa vuota.The query parameter is null or is an empty string.

Esempi

L'esempio in questo argomento è basato sul modello Sales di AdventureWorks.The example in this topic is based on the AdventureWorks Sales Model. In questo esempio viene creato un nuovo oggetto ObjectQuery<T> contenente i risultati di altre due query.This example creates a new ObjectQuery<T> object that contains the results of two other queries.

int productID1 = 900;
int productID2 = 950;
using (AdventureWorksEntities context =
    new AdventureWorksEntities())
{
    string queryString = @"SELECT VALUE product 
        FROM AdventureWorksEntities.Products 
        AS product WHERE product.ProductID > @productID1";

    ObjectQuery<Product> productQuery =
        new ObjectQuery<Product>(queryString,
            context, MergeOption.NoTracking);

    string queryString2 = @"SELECT VALUE product 
        FROM AdventureWorksEntities.Products 
        AS product WHERE product.ProductID > @productID2";

    ObjectQuery<Product> productQuery2 =
        new ObjectQuery<Product>(queryString2,
            context, MergeOption.NoTracking);

    ObjectQuery<Product> productQuery3 =
        productQuery.Intersect(productQuery2);

    productQuery3.Parameters.Add(new ObjectParameter("productID1", productID1));
    productQuery3.Parameters.Add(new ObjectParameter("productID2", productID2));

    Console.WriteLine("Result of Intersect");
    Console.WriteLine("------------------");

    // Iterate through the collection of Product items
    // after the Intersect method was called.
    foreach (Product result in productQuery3)
    {
        Console.WriteLine("Product Name: {0}", result.ProductID);
    }
}
Dim productID1 = 900
Dim productID2 = 950

Using context As New AdventureWorksEntities()
    Dim queryString As String = "SELECT VALUE product FROM AdventureWorksEntities.Products " & _
            " AS product WHERE product.ProductID > @productID1"

    Dim productQuery As New ObjectQuery(Of Product)(queryString, context, MergeOption.NoTracking)

    Dim queryString2 As String = "SELECT VALUE product FROM AdventureWorksEntities.Products " & _
        " AS product WHERE product.ProductID > @productID2"

    Dim productQuery2 As New ObjectQuery(Of Product)(queryString2, context, MergeOption.NoTracking)

    Dim productQuery3 As ObjectQuery(Of Product) = productQuery.Intersect(productQuery2)
    productQuery3.Parameters.Add(New ObjectParameter("productID1", productID1))
    productQuery3.Parameters.Add(New ObjectParameter("productID2", productID2))

    Console.WriteLine("Result of Intersect")
    Console.WriteLine("------------------")

    ' Iterate through the collection of Product items 
    ' after the Intersect method was called. 
    For Each result As Product In productQuery3
        Console.WriteLine("Product Name: {0}", result.ProductID)
    Next
End Using

Commenti

L'oggetto query fornito che definisce i risultati da includere deve essere dello stesso tipo o di un tipo compatibile ObjectQuery<T>con.The supplied query that defines results to include must be of the same type or of a type that is compatible with the ObjectQuery<T>.

I parametri definiti nell'oggetto fornito query vengono uniti con i parametri definiti ObjectQuery<T> nell'istanza di.Parameters that are defined in the supplied query are merged with parameters that are defined in the ObjectQuery<T> instance. I parametri devono essere univoci nell'oggetto ObjectParameterCollection combinato.Parameters must be unique in the combined ObjectParameterCollection. Nella raccolta combinata non possono essere presenti due parametri con lo stesso nome.There cannot be two parameters in the combined collection with the same name. Per ulteriori informazioni, vedere Generatore di query Methods.For more information, see Query Builder Methods.

La query risultante eredita la connessione dall'istanza di ObjectQuery<T> su cui è stato chiamato il metodo Intersect.The resulting query inherits the connection from the ObjectQuery<T> instance on which the Intersect method was called.

Si applica a

Vedi anche