Expressions in LINQ to Entities Queries
An expression is a fragment of code that can be evaluated to a single value, object, method, or namespace. Expressions can contain a literal value, a method call, an operator and its operands, or a simple name. Simple names can be the name of a variable, type member, method parameter, namespace or type. Expressions can use operators that in turn use other expressions as parameters, or method calls whose parameters are in turn other method calls. Therefore, expressions can range from simple to very complex.
In LINQ to Entities queries, expressions can contain anything allowed by the types within the System.Linq.Expressions namespace, including lambda expressions. The expressions that can be used in LINQ to Entities queries are a superset of the expressions that can be used to query the Entity Framework. Expressions that are part of queries against the Entity Framework are limited to operations supported by ObjectQuery<T> and the underlying data source.
In the following example, the comparison in the Where clause is an expression:
Using AWEntities As New AdventureWorksEntities()
Dim sales As ObjectQuery(Of SalesOrderHeader) = AWEntities.SalesOrderHeader
Dim salesInfo = _
From s In sales _
Where s.TotalDue >= 200 _
Select s.SalesOrderID
Console.WriteLine("Sales order info:")
For Each orderNumber As Integer In salesInfo
Console.WriteLine("Order number: " & orderNumber)
Next
End Using
using (AdventureWorksEntities AWEntities = new AdventureWorksEntities())
{
ObjectQuery<SalesOrderHeader> sales = AWEntities.SalesOrderHeader;
IQueryable<int> salesInfo =
from s in sales
where s.TotalDue >= 200
select s.SalesOrderID;
Console.WriteLine("Sales order info:");
foreach (int orderNumber in salesInfo)
{
Console.WriteLine("Order number: " + orderNumber);
}
}
Note
Specific language constructs, such as C# unchecked, have no meaning in LINQ to Entities.