Accepted answer

Just set the body to be the second parameter, p2. You already have the reference.

var p1 = Expression.Parameter(relationshipItems.ElementType, "i");
var p2 = Expression.Parameter(instanceEntities.ElementType, "o");
var body = p2;
var lambda = Expression.Lambda(body, p1, p2);


If, for the sake of example, we assume p1 is an int and p2 is a string then this:

var p1=Expression.Parameter(typeof(int),"i");
var p2=Expression.Parameter(typeof(string),"o");

var lambda=Expression.Lambda(p2,p1,p2);
var function=lambda.Compile() as Func<int,string,string>;
var result=function(10,"hello");

will generate a function that returns "o". The lambda will return the value in the last expression in its body, which in the above example is just the parameter p2.

Related Articles