I would like to see if it I possible to AndAlso two expressions of type dynamic. This sample is very simplistic compared to what I am actually doing but it gets the point across. All examples I have seen using PedicateBuilder or ExpressionBuilder require the expressions to be of type bool and are generally being used as "Where" clauses. This one is returning a new, dynamic object.
public class MyObject
{
public string Name { get; set; }
public string Value { get; set; }
public string Comment { get; set; }
}
Expression<Func<MyObject, dynamic>> e1 = i => new { MyName = i.Name };
Expression<Func<MyObject, dynamic>> e2 = i => new { MyValue = i.Value };
I am looking to get back a combined expression of e1 && e2. Am I going about this the right way?
Update 1: I have tried this answer but it expects input as MemberInitExpression and I have NewExpression which does not contain the bindings needed
Given
You can create a new Anonymous Type that will have both properties. I will create it manually:
Then you can extract the parameters and bodies from the
e1ande2and use them to call the new types constructor:This code assumes the existence of a standard
ExpressionVisitorextension that replaces any occurrence of oneExpressionwith another asReplace(). You can write your own easily or us the EF CoreReplacingExpressionVisitorclass to create one.