Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All

Comparison Expressions

A comparison expression checks whether a constant value, property value, or method result is equal, not equal, greater than, or less than another value. If a particular comparison is not valid for LINQ to Entities, an exception will be thrown. All comparisons, both implicit and explicit, require that all components are comparable in the data source. Comparison expressions are frequently used in Where clauses for restricting the query results.  

The following example in query expression syntax shows a query that returns results where the sales order number is equal to "SO43663":

string salesOrderNumber = "SO43663";
using (AdventureWorksEntities context = new AdventureWorksEntities())
{
    IQueryable<SalesOrderHeader> salesInfo =
        from s in context.SalesOrderHeaders
        where s.SalesOrderNumber == salesOrderNumber
        select s;

    Console.WriteLine("Sales info-");
    foreach (SalesOrderHeader sale in salesInfo)
    {
        Console.WriteLine("Sales ID: " + sale.SalesOrderID);
        Console.WriteLine("Ship date: " + sale.ShipDate);
    }
}

The following example in method-based query syntax shows a query that returns results where the sales order number is equal to "SO43663":

    string salesOrderNumber = "SO43663";
    IQueryable<SalesOrderHeader> salesInfo =
        context.SalesOrderHeaders
        .Where(s => s.SalesOrderNumber == salesOrderNumber)
        .Select(s => s);

    Console.WriteLine("Sales info-");
    foreach (SalesOrderHeader sale in salesInfo)
    {
        Console.WriteLine("Sales ID: " + sale.SalesOrderID);
        Console.WriteLine("Ship date: " + sale.ShipDate);
    }
}

The following example in query expression syntax shows a query that returns sales order information where the ship date is equal to July 8, 2001:

using (AdventureWorksEntities context = new AdventureWorksEntities())
{
    DateTime dt = new DateTime(2001, 7, 8);

    IQueryable<SalesOrderHeader> salesInfo =
        from s in context.SalesOrderHeaders
        where s.ShipDate == dt
        select s;

    Console.WriteLine("Orders shipped on August 7, 2001:");
    foreach (SalesOrderHeader sale in salesInfo)
    {
        Console.WriteLine("Sales ID: " + sale.SalesOrderID);
        Console.WriteLine("Total due: " + sale.TotalDue);
        Console.WriteLine();
    }
}

The following example in method-based query syntax shows a query that returns sales order information where the ship date is equal to July 8, 2001:

using (AdventureWorksEntities context = new AdventureWorksEntities())
{
    DateTime dt = new DateTime(2001, 7, 8);

    IQueryable<SalesOrderHeader> salesInfo =
        context.SalesOrderHeaders
        .Where(s => s.ShipDate == dt)
        .Select(s => s);

    Console.WriteLine("Orders shipped on August 7, 2001:");
    foreach (SalesOrderHeader sale in salesInfo)
    {
        Console.WriteLine("Sales ID: " + sale.SalesOrderID);
        Console.WriteLine("Total due: " + sale.TotalDue);
        Console.WriteLine();
    }
}

Expressions that yield a constant are converted at the server, and no attempt to do local evaluation is performed. The following example uses an expression in the Where clause that yields a constant.

Decimal totalDue = 200 + 3;
using (AdventureWorksEntities context = new AdventureWorksEntities())
{
    IQueryable<string> salesInfo =
        from s in context.SalesOrderHeaders
        where s.TotalDue >= totalDue
        select s.SalesOrderNumber;

    Console.WriteLine("Sales order numbers:");
    foreach (string orderNum in salesInfo)
    {
        Console.WriteLine(orderNum);
    }
}

LINQ to Entities does not support using a user class as a constant. However, a property reference on a user class is considered a constant, and will be converted to a command tree constant expression and executed on the data source.

class AClass { public int ID;}

using (AdventureWorksEntities context = new AdventureWorksEntities())
{
    AClass aClass = new AClass();
    aClass.ID = 43663;

    IQueryable<SalesOrderHeader> salesInfo =
        from s in context.SalesOrderHeaders
        where s.SalesOrderID == aClass.ID
        select s;

    Console.WriteLine("Order info-");
    foreach (SalesOrderHeader sale in salesInfo)
    {
        Console.WriteLine("Sales order number: " + sale.SalesOrderNumber);
        Console.WriteLine("Total due: " + sale.TotalDue);
        Console.WriteLine();
    }
}

Methods that return a constant expression are not supported. The following example contains a method in the Where clause that returns a constant. This example will throw an exception at run time.

using (AdventureWorksEntities context = new AdventureWorksEntities())
{
    MyClass2 myClass = new MyClass2();


    //Throws a NotSupportedException
    IQueryable<SalesOrderHeader> salesInfo =
        from s in context.SalesOrderHeaders
        where s.SalesOrderID == myClass.returnInt()
        select s;

    Console.WriteLine("Order info-");
    try
    {
        foreach (SalesOrderHeader sale in salesInfo)
        {
            Console.WriteLine("Sales order number: " + sale.SalesOrderNumber);
            Console.WriteLine("Total due: " + sale.TotalDue);
            Console.WriteLine();
        }
    }
    catch (NotSupportedException ex)
    {
        Console.WriteLine("Exception: {0}", ex.Message);
    }
}

See Also

Community Additions

ADD
Show:
© 2015 Microsoft