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

定义

通过根据另一个对象查询的结果排除结果的方式限制查询结果。Limits the query results by excluding results based on the results of another object query.

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

参数

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

一个 ObjectQuery<T>,表示要从查询中排除的结果。An ObjectQuery<T> that represents the results to exclude from the query.

返回

新的 ObjectQuery<T> 实例,等效于根据指定的 query 应用了 EXCEPT 的原始实例。A new ObjectQuery<T> instance that is equivalent to the original instance with EXCEPT applied based on the specified query.

异常

query 参数为 null 或空字符串。The query parameter is null or an empty string.

示例

此示例基于AdventureWorks 销售模型This example is based on the AdventureWorks Sales Model. 该示例使用 Except 方法创建新的 ObjectQuery<T> 对象,然后循环访问新查询的结果。The example uses Except method to create a new ObjectQuery<T> object and then iterates through the result of the new query.

int productID = 900;
using (AdventureWorksEntities context =
    new AdventureWorksEntities())
{
    string queryString = @"SELECT VALUE product 
        FROM AdventureWorksEntities.Products AS product";

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

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

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

    productQuery2.Parameters.Add(new ObjectParameter("productID", productID));

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

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

    // Iterate through the collection of Product items
    // after the Except method was called.
    foreach (Product result in productQuery3)
        Console.WriteLine("Product Name: {0}",
            result.ProductID);
}
Dim productID = 900
Using context As New AdventureWorksEntities()
    Dim queryString As String = "SELECT VALUE product FROM AdventureWorksEntities.Products AS product"
    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 < @productID"
    Dim productQuery2 As New ObjectQuery(Of Product)(queryString2, context, MergeOption.NoTracking)
    productQuery2.Parameters.Add(New ObjectParameter("productID", productID))

    Dim productQuery3 As ObjectQuery(Of Product) = productQuery.Except(productQuery2)

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

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

注解

提供query的用于定义要排除的结果的类型必须为与兼容ObjectQuery<T>的类型或类型。The supplied query that defines results to exclude must be of the same type or of a type that is compatible with the ObjectQuery<T>.

在提供query的中定义的参数将与ObjectQuery<T>在实例中定义的参数合并。Parameters that are defined in the supplied query are merged with parameters that are defined in the ObjectQuery<T> instance. 参数在组合后的 ObjectParameterCollection 中必须是唯一的。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.

生成的查询从 ObjectQuery<T> 实例(调用了它的 Except)继承连接。The resulting query inherits the connection from the ObjectQuery<T> instance on which Except was called.

适用于

另请参阅