ObjectQuery<T>.Intersect(ObjectQuery<T>) 方法

定義

透過僅包含存在另一個物件查詢中的結果,限制查詢結果。

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)

參數

query
ObjectQuery<T>

ObjectQuery<T>,它代表要包含在查詢中的結果。

傳回

新的 ObjectQuery<T> 執行個體,它就相當於根據指定的 query 套用了 INTERSECT 的原始執行個體。

例外狀況

query 參數是 null,或者空字串。

範例

這個範例會建立新的 ObjectQuery<T> 物件,其中包含其他兩個查詢的結果。

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

備註

提供的 query ,定義要包含的結果必須是與 相容的 ObjectQuery<T>相同型別或型別。

所提供 query 中定義的參數會與 實例中 ObjectQuery<T> 定義的參數合併。 參數在結合的 ObjectParameterCollection 中必須是唯一的。 結合的集合中不能有兩個參數同名。 如需詳細資訊,請參閱 查詢產生器方法

結果查詢會從在上面呼叫 ObjectQuery<T> 方法的 Intersect 執行個體繼承連接。

適用於

另請參閱