I want to generate expression by string parameter,some code like:
private Expression> Generate(string orderby)
{
switch (orderb
Use a generic method. Since lambda expressions can only be assigned to strongly typed delegates or expressions, we must use an according temp. Then we can assign this temp to a variable typed as object. Finally we can return the result by casting to the result type.
public Expression> Generate(string orderby)
{
object result;
switch (orderby) {
case "Time":
Expression> temp1 = t => t.Time;
result = temp1;
break;
case "Money":
Expression> temp2 = t => t.RewardMoney;
result = temp2;
break;
default:
Expression> temp3 = t => t.Id;
result = temp3;
break;
}
return (Expression>)result;
}