I am not familiar with NHibernate, but I can try to explain the differences between the two expressions.
1. The main difference is the use of Expression.Invoke in Example # 2, which creates an InvocatonExpression that "represents an expression that applies a delegate or lambda expression to the list of argument expressions." This introduces a kind of indirect attitude - fluent,
Sample No. 1:
y => y.Address.City == geoObject.Name;
whereas example 2 looks something like this:
x => new Func<PlainAddress, string>(y => y.Address.City).Invoke(x) == geoObject.Name
You can get the second sample closer to the first using Expression.Property to create the corresponding MemberExpression :
var predicate = Expression.Lambda<Func<PlainAddress, bool>>( Expression.Equal( Expression.Property(Expression.Property(x, "Address"), "City"), Expression.Constant(geoObject.Name)), x);
Now it is closer to:
x => x.Address.City == AsStringLiteral(geoObject.Name)
(I assume this will be enough for the predicate to work with NHibernate.)
2. Another difference, of course, is the use of Expression.Constant in example # 2, which will readily evaluate the value of geoObject.Name and turn it into a literal in the expression tree. You will need much more work if you want to "emulate" the rise of the geoObject variable (or perhaps this ?), As in example No. 1.
source share