I want to generate expression by string parameter,some code like:
private Expression> Generate(string orderby)
{
switch (orderb
You could try converting the Generate
method in a generic method:
private Expression> Generate(string orderby)
{
switch (orderby)
{
case "Time":
return t => t.Time;
case "Money":
return t => t.RewardMoney;
default:
return t => t.Id;
}
}
So, if you call this method, you need to specify the type of the property that you want to order by:
_context.Items.OrderBy(Generate("Money"));
Now remember that TResult
can only be a primitive type or enumeration type.