Expressions Cheat Sheet


I started getting really tired of looking up the translation between the ExpressionType and concrete Expression type (they don’t match up), so I created this cheat sheet that has each ExpressionType, derived Expression type and a simple example.  What’s interesting is a few are only available in VB.NET, but no one really wants non-short-circuited Or/And operators, do they?  You can download the PDF version, or just view the big list in all its glory:

ExpressionType Type Example
Add BinaryExpression
int i = 2, j = 3;
Expression<Func<int>> example = () => i + j;

</td> </tr>

AddChecked BinaryExpression
int i = Int32.MaxValue, j = 1;
Expression<Func<int>> example = () => checked(i + j);

</td> </tr>

And BinaryExpression
Dim i As Boolean = True, j As Boolean = False
Dim sample As Expression(Of Func(Of Boolean)) = _
 Function() i And j

</td> </tr>

AndAlso BinaryExpression
bool i = true, j = false;
Expression<Func<bool>> example = () => i && j;

</td> </tr>

ArrayLength UnaryExpression
int[] values = {1, 2, 3};
Expression<Func<int>> example = () => values.Length;

</td> </tr>

ArrayIndex MethodCallExpression
int[] values = {1, 2, 3};
Expression<Func<int>> example = () => values[1];

</td> </tr>

Call MethodCallExpression
var sample = new Sample();
Expression<Func<int>> example = 
    () => sample.Calc();
Coalesce BinaryExpression
int? i = null, j = 5;
Expression<Func<int?>> example = () => i ?? j;

</td> </tr>

Conditional ConditionalExpression
int i = 3, j = 5;
bool k = false;
Expression<Func<int?>> example = () => k ? i : j;

</td> </tr>

Constant ConstantExpression
Expression<Func<int>> example = () => 5;

</td> </tr>

Convert UnaryExpression
int i = 5;
object j = i;
Expression<Func<int>> example = () => (int) j;

</td> </tr>

ConvertChecked UnaryExpression
long i = 5;
Expression<Func<int>> example = () => checked((int)i);

</td> </tr>

Divide BinaryExpression
int i = 21, j = 3;
Expression<Func<int>> example = () => i / j;

</td> </tr>

Equal BinaryExpression
int i = 21, j = 3;
Expression<Func<bool>> example = () => i == j;

</td> </tr>

ExclusiveOr BinaryExpression
int i = 12, j = 7;
Expression<Func<int>> example = () => i ^ j;

</td> </tr>

GreaterThan BinaryExpression
int i = 12, j = 7;
Expression<Func<bool>> example = () => i > j;

</td> </tr>

GreaterThanOrEqual BinaryExpression
int i = 12, j = 7;
Expression<Func<bool>> example = () => i >= j;

</td> </tr>

Invoke InvocationExpression
Expression<Func<int, int, int>> expr = (i, j) => i + j;
Expression invoke = Expression.Invoke(
    expr, 
    Expression.Constant(5), 
    Expression.Constant(4));
Expression<Func<int>> example = 
    Expression.Lambda<Func<int>>(invoke);

</td> </tr>

Lambda LambdaExpression
Expression<Func<int>> example = 
    Expression.Lambda<Func<int>>(Expression.Constant(5));

</td> </tr>

LeftShift BinaryExpression
int i = 8;
Expression<Func<int>> example = () => i << 1;

</td> </tr>

LessThan BinaryExpression
int i = 12, j = 7;
Expression<Func<bool>> example = () => i < j;

</td> </tr>

LessThanOrEqual BinaryExpression
int i = 12, j = 7;
Expression<Func<bool>> example = () => i <= j;

</td> </tr>

ListInit ListInitExpression
Expression<Func<List<int>>> example =
    () => new List<int> {1, 2, 3};

</td> </tr>

MemberAccess MemberExpression
var c = new Customer {Name = "Bob"};
Expression<Func<string>> example = () => c.Name;

</td> </tr>

MemberInit MemberInitExpression
Expression<Func<Customer>> example =
    () => new Customer {Name = "Bob"};

</td> </tr>

Modulo BinaryExpression
int i = 5, j = 3;
Expression<Func<int>> example = () => i % j;

</td> </tr>

Multiply BinaryExpression
int i = 5, j = 3;
Expression<Func<int>> example = () => i * j;

</td> </tr>

MultiplyChecked BinaryExpression
int i = 5, j = 3;
Expression<Func<int>> example = () => checked(i * j);

</td> </tr>

Negate UnaryExpression
int i = 5;
Expression<Func<int>> example = () => -i;

</td> </tr>

UnaryPlus UnaryExpression
var m = new Money { Amount = -10m };
Expression<Func<Money>> example = () => +m;

</td> </tr>

NegateChecked UnaryExpression
int i = 5;
Expression<Func<int>> example = () => checked(-i);

</td> </tr>

New NewExpression
Expression<Func<Customer>> example = 
    () => new Customer();

</td> </tr>

NewArrayInit NewArrayExpression
Expression<Func<int[]>> example = 
    () => new[] {1, 2, 3};

</td> </tr>

NewArrayBounds NewArrayExpression
Expression<Func<int[]>> example = () => new int[10];

</td> </tr>

Not UnaryExpression
bool val = true;
Expression<Func<bool>> example = () => !val;

</td> </tr>

NotEqual BinaryExpression
int i = 4, j = 7;
Expression<Func<bool>> example = () => i != j;

</td> </tr>

Or BinaryExpression
Dim i As Boolean = True, j As Boolean = False
Dim sample As Expression(Of Func(Of Boolean)) = _
 Function() i Or j

</td> </tr>

OrElse BinaryExpression
bool i = true, j = false;
Expression<Func<bool>> example = () => i || j;

</td> </tr>

Parameter ParameterExpression
// (i, j) => i + j;
ParameterExpression param1 = 
    Expression.Parameter(typeof (int), "i");
ParameterExpression param2 = 
    Expression.Parameter(typeof (int), "j");
var addExpression = Expression.Add(param1, param2);
var example = Expression.Lambda<Func<int, int, int>>(
    addExpression, param1, param2);

</td> </tr>

Power BinaryExpression
Dim i As Integer = 3, j As Integer = 2
Dim sample As Expression(Of Func(Of Integer)) = _
 Function() i ^ j

</td> </tr>

Quote UnaryExpression
int i = 3, j = 2;
Expression<Func<int>> inner = () => i * j;
var quoted = Expression.Quote(inner);
Expression<Func<Expression<Func<int>>>> example = 
    Expression.Lambda<Func<Expression<Func<int>>>>(quoted);

</td> </tr>

RightShift BinaryExpression
int i = 8;
Expression<Func<int>> example = () => i >> 1;

</td> </tr>

Subtract BinaryExpression
int i = 8, j = 5;
Expression<Func<int>> example = () => i - j;

</td> </tr>

SubtractChecked BinaryExpression
int i = 8, j = 5;
Expression<Func<int>> example = () => checked(i - j);

</td> </tr>

TypeAs UnaryExpression
var c = new Customer {Name = "Bob"};
Expression<Func<Person>> example = () => c as Person;

</td> </tr>

TypeIs TypeBinaryExpression
var c = new Customer {Name = "Bob"};
Expression<Func<bool>> example = () => c is int;

</td> </tr> </tbody> </table>

This is only for C# 3.0, the .NET 4.0 list should grow quite a bit.

The Filter-ViewData anti-pattern