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 方法)继承连接。

适用于

另请参阅