If you ask how to use an expression object, you need to compile it first. The compiled expression itself is just a delegate, so you need to pass the data you need to it. For example. here is an example of what you asked:
public int GetMaxValue(Expression<Func<MyData, int>> action)
{
if (MyDatas.Count == 0)
{
throw new InvalidOperationException("Sequence contains no elements");
}
Func<MyData, int> func = action.Compile();
int max = int.MinValue;
foreach (MyData data in MyDatas)
{
max = Math.Max(max, func(data));
}
return max;
}
source
share