LINQ Expressions - Overview

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. A simple name 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 which parameters are in turn other method calls. Therefore, expressions can range from simple to very complex.

In the following example, the comparison in the Where clause is an expression:

C#


    using ( EntitiesModel dbContext = new EntitiesModel() )
    {
       IQueryable<int> products = from product in dbContext.Products
                                  where product.UnitPrice > 20
                                  select product.ProductID;
       foreach ( int id in products )
       {
           Console.WriteLine( id );
       }
    }

VB


    Using dbContext As New EntitiesModel()
     Dim products As IQueryable(Of Integer) = From product In dbContext.Products
                                              Where product.UnitPrice > 20
                                              Select product.ProductID
     For Each id As Integer In products
      Console.WriteLine(id)
     Next id
    End Using

In This Section:

See Also