from a Lambda Expression.
With regards to Expression Trees I know the common practice
is to build from the bottom up but reversed when coded i.e.
ParameterExpression 1st
MemberExpression 2nd
ConstantExpression 3rd
BinaryExpression 4th
LambdaExpression 5th - for executing and compiling to a delegate.
With expressions 1-4 being used to form the Body of the Expression Tree.
My question is from the code example below I cant seem
to see how the Expressions 1-4 are being used to form the Body?
It looks like only some are used?
Regards,
Code
public static class PredicateBuilderCode
{Code
private static readonly MethodInfo asNonUnicodeMethodInfo =Code
typeof(EntityFunctions).GetMethod("AsNonUnicode");Code
private static readonly MethodInfo stringEqualityMethodInfo =Code
typeof(string).GetMethod("op_Equality");Code
Code
public static Expressionbool>> ContainsNonUnicodeString(Code
IEnumerablestring> source,Code
Expressionstring>> expression)Code
{Code
if (source == null) throw new ArgumentNullException("source");Code
if (expression == null) throw new ArgumentNullException("expression");Code
Code
Expression predicate = null;Code
foreach (string value in source)Code
{Code
var fragment = Expression.Equal(Code
expression.Body,Code
Expression.Call(null,Code
asNonUnicodeMethodInfo,Code
Expression.Constant(value, typeof(string))),Code
false,Code
stringEqualityMethodInfo);Code
if (predicate == null)Code
{Code
predicate = fragment;Code
}Code
elseCode
{Code
predicate = Expression.OrElse(predicate, fragment);Code
}Code
}Code
Code
return Expression.Lambdabool>>(predicate,Code
((LambdaExpression)expression).Parameters);Code
}Code
}
VulpesRank #55 · 29.2K reputationPosted May 15, 2013, 6:05 AM EDT
I haven't had time to test it but it seems to be building and returning an expression tree which provides a predicate to test whether or not a collection of strings contains a non-unicode string - how it cannot do, I don't know as all strings in .NET are unicode!
One of the parameters is itself an expression tree and the whole thing is very general.
You can see some of the usual elements in there such as Expression.Constant which holds the value of the current string in the collection and Expression.OrElse which OR's the individual predicates together.
However, no ParameterExpression is being built. Instead the 'expression' argument's Parameters collection is being passed through to the lambda expression in the final return statement:
return Expression.Lambda
Expression tree code is never easy to understand (even worse than reflection) and it's probably best not to expect it to conform to any particular sequence of steps when trying to read it.
Guest UserRank #4,868 · 321 reputationPosted May 15, 2013, 6:44 AM EDT
the usage of the code would be as follows:-
var values = new[] { "a", "b", "c" };
var q = context.Customers.Where( PredicateBuilder.ContainsNonUnicodeString
VulpesRank #55 · 29.2K reputationPosted May 15, 2013, 2:37 PM EDT
Anyway, I see now what they mean by non-unicode string - they're talking about the type of column in the underlying database which need not, of course, be unicode.