Partilhar via


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":

Dim salesOrderNumber = "SO43663"
Using context As New AdventureWorksEntities()
    Dim salesInfo = _
        From s In context.SalesOrderHeaders _
        Where s.SalesOrderNumber = salesOrderNumber _
        Select s

    Console.WriteLine("Sales info-")
    For Each sale As SalesOrderHeader In salesInfo
        Console.WriteLine("Sales ID: " & sale.SalesOrderID)
        Console.WriteLine("Ship date: " & sale.ShipDate)
    Next
End Using
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":

Dim salesOrderNumber = "SO43663"
Using context As New AdventureWorksEntities()
    Dim salesInfo = _
        context.SalesOrderHeaders _
            .Where(Function(s) s.SalesOrderNumber = salesOrderNumber) _
            .Select(Function(s) s)

    Console.WriteLine("Sales info-")
    For Each sale As SalesOrderHeader In salesInfo
        Console.WriteLine("Sales ID: " & sale.SalesOrderID)
        Console.WriteLine("Ship date: " & sale.ShipDate)
    Next
End Using
    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 context As New AdventureWorksEntities()
    Dim dt As DateTime = New DateTime(2001, 7, 8)
    Dim salesInfo = _
        From s In context.SalesOrderHeaders _
        Where s.ShipDate = dt _
        Select s

    Console.WriteLine("Orders shipped on August 7, 2001:")
    For Each sale As SalesOrderHeader In salesInfo
        Console.WriteLine("Sales ID: " & sale.SalesOrderID)
        Console.WriteLine("Total due: " & sale.TotalDue)
        Console.WriteLine()
    Next
End Using
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 context As New AdventureWorksEntities()
    Dim dt As DateTime = New DateTime(2001, 7, 8)

    Dim salesInfo = _
        context.SalesOrderHeaders _
        .Where(Function(s) s.ShipDate = dt) _
        .Select(Function(s) s)

    Console.WriteLine("Orders shipped on August 7, 2001:")
    For Each sale As SalesOrderHeader In salesInfo
        Console.WriteLine("Sales ID: " & sale.SalesOrderID)
        Console.WriteLine("Total due: " & sale.TotalDue)
        Console.WriteLine()
    Next
End Using
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.

Dim totalDue = 200 + 3
Using context As New AdventureWorksEntities()
    Dim salesInfo = _
        From s In context.SalesOrderHeaders _
        Where s.TotalDue >= totalDue _
        Select s.SalesOrderNumber

    Console.WriteLine("Sales order numbers:")
    For Each orderNum As String In salesInfo
        Console.WriteLine(orderNum)
    Next
End Using
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 ID As Integer
End Class
class AClass { public int ID;}
Using context As New AdventureWorksEntities()
    Dim aClass As AClass = New aClass()
    aClass.ID = 43663

    Dim salesInfo = _
        From s In context.SalesOrderHeaders _
        Where s.SalesOrderID = aClass.ID _
        Select s

    Console.WriteLine("Order info-")
    For Each sale As SalesOrderHeader In salesInfo
        Console.WriteLine("Sales order number: " & sale.SalesOrderNumber)
        Console.WriteLine("Total due: " & sale.TotalDue)
        Console.WriteLine()
    Next
End Using
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 context As New AdventureWorksEntities()
    Dim aClass2 As AClass2 = New aClass2()

    ' Throws a NotSupportedException.
    Dim salesInfo = _
        From s In context.SalesOrderHeaders _
        Where s.SalesOrderID = aClass2.returnInt() _
        Select s

    Console.WriteLine("Order info-")
    Try
        For Each sale As SalesOrderHeader In salesInfo
            Console.WriteLine("Sales order number: " & sale.SalesOrderNumber)
            Console.WriteLine("Total due: " & sale.TotalDue)
            Console.WriteLine()
        Next
    Catch ex As NotSupportedException
        Console.WriteLine("Exception: {0}", ex.Message)
    End Try
End Using
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

Concepts

Expressions in LINQ to Entities Queries