Introduction
For the most part, I’ll assume you have a basic idea of what a Lambda Expression is. I will provide a very quick overview, but the focus is on the difference between Lambda Expressions that are emitted as code and those that are emitted as expression trees and what their different uses are.
What’s a Lambda Expression?
Lambda Expressions are based on function programming; they have been added to .NET. A Lambda Expression is essentially a very compressed function often used where a delegate would be used, and they have a precise syntax, which is roughly:
Function(arg1, arg2...argn) expression
You can explicitly type the parameters—that is, provide the argument types or leave them unspecified and the compiler will figure it out. The expression part is a statement such as a + b, or s.Length. The Return keyword is implicit.
Compiled Code Versus Expression Trees
When you use Lambda Expressions, the compiler might emit compiled code or an expression tree. If the Lambda Expression is assigned to a variable, field, or delegate the compiler emits executable IL—executable code. If the Lambda Expression is assigned to a variable, field, or parameter whose type is System.Linq.Expressions.Expression(Of TDelegate), code that represents an expression tree is emitted.
Common usage will be such that executable IL is emitted. However, if you are implementing or using an API that converts Lambda Expressions to something else, like T-SQL, then expression trees will be used.
Exploring an Expression Tree
An expression tree is the Lambda Expression broken up into explorable chunks (or nodes) that describe expression. Common members of the Expression class are Body, NodeType, Type, and Parameters.
The Body is the statement that makes up the expression. The NodeType is an instance of the enumeration ExpressionType that can contain values such Lambda, Divide, Not, Power, and much more. (Refer to the MSDN help for a complete list.) The Expression.Type is the underling type of the Lambda Expression mapped to a generic delegate like Func(Of String, Integer). The Parameters property is a collection of ParameterExpression objects that contain information about the parameter type and name (see Listing 1).
Listing 1: Creating and exploring an instance of an Expression as well as compiling it at runtime and using the underlying Lambda Expression.
Imports System.Linq.Expressions Module Module1 Sub Main() Dim Exp As Expression(Of Func(Of String, Integer)) = _ Function(s) s.Length Console.WriteLine("Body: " + Exp.Body.ToString()) Console.WriteLine("Node Type: " + Exp.NodeType.ToString()) Console.WriteLine("Type: " + Exp.Type.ToString()) For Each parm In Exp.Parameters Console.WriteLine("Parm: " + parm.ToString()) Next Dim anon = Exp.Compile() Console.WriteLine( _ "Compiled and called, string {0} is {1} characters.", _ "hello world", anon("hello world").ToString()) Console.ReadLine() Dim numbers = New Integer() {1, 2, 3, 4, 5, 6, 7} Dim all = From num In numbers End Sub End Module
If you are confused about the fragment Func(Of String, Integer), refer to October 2007’s article on generic delegates.
Summary
Lambda Expressions can emitted as compiled code—which is probably the most frequent use scenario—or expression trees. Expression trees are useful when Lambda Expressions have to be converted to something else, like T-SQL, such as is the case with LINQ for Data.
About the Author
Paul Kimmel is the VB Today columnist for www.codeguru.com and has written several books on object-oriented programming and .NET. Check out his upcoming book LINQ Unleashed for C# due in Spring 2008. You may contact him for technology questions at pkimmel@softconcepts.com.
If you are interested in joining or sponsoring a .NET Users Group, check out www.glugnet.org. Glugnet opened a users group branch in Flint, Michigan in August 2007. If you are interested in attending, check out the www.glugnet.org web site for updates.
Copyright © 2007 by Paul T. Kimmel. All Rights Reserved.