Bewerken

Delen via


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

Definition

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)

Parameters

query
ObjectQuery<T>

An ObjectQuery<T> that represents the results to include in the query.

Returns

A new ObjectQuery<T> instance that is equivalent to the original instance with INTERSECT applied based on the specified query.

Exceptions

The query parameter is null or is an empty string.

Examples

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

Remarks

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>.

Parameters that are defined in the supplied query are merged with parameters that are defined in the ObjectQuery<T> instance. Parameters must be unique in the combined ObjectParameterCollection. There cannot be two parameters in the combined collection with the same name. For more information, see Query Builder Methods.

The resulting query inherits the connection from the ObjectQuery<T> instance on which the Intersect method was called.

Applies to

See also