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":
Using AWEntities As New AdventureWorksEntities()
Dim sales As ObjectQuery(Of SalesOrderHeader) = AWEntities.SalesOrderHeader
Dim salesInfo = _
From s In sales _
Where s.SalesOrderNumber = "SO43663" _
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
using (AdventureWorksEntities AWEntities = new AdventureWorksEntities())
{
ObjectQuery<SalesOrderHeader> sales = AWEntities.SalesOrderHeader;
IQueryable<SalesOrderHeader> salesInfo =
from s in sales
where s.SalesOrderNumber == "SO43663"
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":
Using AWEntities As New AdventureWorksEntities()
Dim salesInfo = _
AWEntities.SalesOrderHeader _
.Where(Function(s) s.SalesOrderNumber = "SO43663") _
.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
IQueryable<SalesOrderHeader> salesInfo =
AWEntities.SalesOrderHeader
.Where(s => s.SalesOrderNumber == "SO43663")
.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 AWEntities As New AdventureWorksEntities()
Dim dt As DateTime = New DateTime(2001, 7, 8)
Dim sales As ObjectQuery(Of SalesOrderHeader) = AWEntities.SalesOrderHeader
Dim salesInfo = _
From s In sales _
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 AWEntities = new AdventureWorksEntities())
{
DateTime dt = new DateTime(2001, 7, 8);
ObjectQuery<SalesOrderHeader> sales = AWEntities.SalesOrderHeader;
IQueryable<SalesOrderHeader> salesInfo =
from s in sales
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 AWEntities As New AdventureWorksEntities()
Dim dt As DateTime = New DateTime(2001, 7, 8)
Dim salesInfo = _
AWEntities.SalesOrderHeader _
.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 AWEntities = new AdventureWorksEntities())
{
DateTime dt = new DateTime(2001, 7, 8);
IQueryable<SalesOrderHeader> salesInfo =
AWEntities.SalesOrderHeader
.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.
Using AWEntities As New AdventureWorksEntities()
Dim sales As ObjectQuery(Of SalesOrderHeader) = AWEntities.SalesOrderHeader
Dim salesInfo = _
From s In sales _
Where s.TotalDue >= 200 + 3 _
Select s.SalesOrderNumber
Console.WriteLine("Sales order numbers:")
For Each orderNum As String In salesInfo
Console.WriteLine(orderNum)
Next
End Using
using (AdventureWorksEntities AWEntities = new AdventureWorksEntities())
{
ObjectQuery<SalesOrderHeader> sales = AWEntities.SalesOrderHeader;
IQueryable<string> salesInfo =
from s in sales
where s.TotalDue >= 200 + 3
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 AWEntities As New AdventureWorksEntities()
Dim aClass As AClass = New aClass()
aClass.ID = 43663
Dim sales As ObjectQuery(Of SalesOrderHeader) = AWEntities.SalesOrderHeader
Dim salesInfo = _
From s In sales _
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 AWEntities = new AdventureWorksEntities())
{
AClass aClass = new AClass();
aClass.ID = 43663;
ObjectQuery<SalesOrderHeader> sales = AWEntities.SalesOrderHeader;
IQueryable<SalesOrderHeader> salesInfo =
from s in sales
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 AWEntities As New AdventureWorksEntities()
Dim aClass2 As AClass2 = New aClass2()
Dim sales As ObjectQuery(Of SalesOrderHeader) = AWEntities.SalesOrderHeader
' Throws a NotSupportedException.
Dim salesInfo = _
From s In sales _
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 AWEntities = new AdventureWorksEntities())
{
MyClass2 myClass = new MyClass2();
ObjectQuery<SalesOrderHeader> sales = AWEntities.SalesOrderHeader;
//Throws a NotSupportedException
IQueryable<SalesOrderHeader> salesInfo =
from s in sales
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);
}
}