Export (0) Print
Expand All

ObjectQuery<T>.Except Method

Limits the query results by excluding results based on the results of another object query.

Namespace:  System.Data.Objects
Assembly:  System.Data.Entity (in System.Data.Entity.dll)

public ObjectQuery<T> Except(
	ObjectQuery<T> query
)

Parameters

query
Type: System.Data.Objects.ObjectQuery<T>

An ObjectQuery<T> that represents the results to exclude from the query.

Return Value

Type: System.Data.Objects.ObjectQuery<T>
A new ObjectQuery<T> instance that is equivalent to the original instance with EXCEPT applied based on the specified query.

ExceptionCondition
ArgumentNullException

The query parameter is null or an empty string.

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

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 (Entity Framework).

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

This example is based on the Adventure Works Sales Model. The example uses Except method to create a new ObjectQuery<T> object and then iterates through the result of the new query.

using (AdventureWorksEntities advWorksContext =
    new AdventureWorksEntities())
{
    try
    {
        string queryString = @"SELECT VALUE Product 
            FROM AdventureWorksEntities.Product AS Product";
        ObjectQuery<Product> productQuery =
            new ObjectQuery<Product>(queryString, 
                advWorksContext, MergeOption.NoTracking);

        string queryString2 = @"SELECT VALUE Product 
            FROM AdventureWorksEntities.Product 
            AS Product WHERE Product.ProductID < 900";
        ObjectQuery<Product> productQuery2 =
            new ObjectQuery<Product>(queryString2, 
                advWorksContext, MergeOption.NoTracking);

        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);
    }
    catch (EntitySqlException ex)
    {
        Console.WriteLine(ex.ToString());
    }
}

Windows 7, Windows Vista, Windows XP SP2, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5 SP1

Community Additions

ADD
Show:
© 2014 Microsoft