Share via

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:

Decimal totalDue = 200;
using (AdventureWorksEntities context = new AdventureWorksEntities())
    IQueryable<int> salesInfo =
        from s in context.SalesOrderHeaders
        where s.TotalDue >= totalDue
        select s.SalesOrderID;

    Console.WriteLine("Sales order info:");
    foreach (int orderNumber in salesInfo)
        Console.WriteLine("Order number: " + orderNumber);
Dim totalDue = 200
Using context As New AdventureWorksEntities()
    Dim salesInfo = _
        From s In context.SalesOrderHeaders _
        Where s.TotalDue >= totalDue _
        Select s.SalesOrderID

    Console.WriteLine("Sales order info:")
    For Each orderNumber As Integer In salesInfo
        Console.WriteLine("Order number: " & orderNumber)
End Using


Specific language constructs, such as C# unchecked, have no meaning in LINQ to Entities.

In This Section

Constant Expressions

Comparison Expressions

Null Comparisons

Initialization Expressions

Relationships, navigation properties and foreign keys

See also