17 using System.Collections.Generic;
19 using System.Linq.Expressions;
36 var parameter = Expression.Parameter(type);
37 var
property = Expression.PropertyOrField(parameter, propertyOrField);
38 var lambda = Expression.Lambda(property, parameter);
60 if (!type.IsBinaryComparison())
62 throw new ArgumentException($
"Provided ExpressionType '{type}' is not a binary comparison.");
65 var left = Expression.Parameter(typeof(T),
"left");
66 var right = Expression.Parameter(typeof(T),
"right");
67 var body = Expression.MakeBinary(type, left, right);
68 var lambda = Expression.Lambda<Func<T, T, bool>>(body, left, right);
79 case ExpressionType.Equal:
80 case ExpressionType.NotEqual:
81 case ExpressionType.LessThan:
82 case ExpressionType.LessThanOrEqual:
83 case ExpressionType.GreaterThan:
84 case ExpressionType.GreaterThanOrEqual:
97 public static IEnumerable<Expression>
AsEnumerable(
this Expression expression)
99 var walker =
new ExpressionWalker();
100 walker.Visit(expression);
101 return walker.Expressions;
110 public static IEnumerable<T>
OfType<T>(
this Expression expression)
113 return expression.AsEnumerable().OfType<T>();
126 return expression.AsEnumerable().OfType<T>().Single();
136 public static T
Single<T>(
this IEnumerable<Expression> expressions)
139 return expressions.OfType<T>().Single();
142 private class ExpressionWalker : ExpressionVisitor
144 public readonly HashSet<Expression> Expressions =
new HashSet<Expression>();
145 public override Expression Visit(Expression node)
147 Expressions.Add(node);
148 return base.Visit(node);