I want to initialize the Expression Call outside the if else condition. As i have to use it to generate expression body because i have two different type coming from database i.e int and int?. My Code is below. I am getting error to instantiate the toString object.
var parameterExp = Expression.Parameter(typeof(T), "type");
var propertyExp = Expression.Property(parameterExp, propertyName);
MethodInfo method = typeof(string).GetMethod(methodType, new[] { typeof(string) });
var searchValue = Expression.Constant(propertyValue.ToLower(), typeof(string));
var toString = new MethodCallExpression();
if (propertyName == nameof(CustomerListDto.Id))
{
toString = Expression.Call(propertyExp, typeof(int).GetMethod("ToString", System.Type.EmptyTypes));
}
else
{
toString = Expression.Call(propertyExp, typeof(int?).GetMethod("ToString", System.Type.EmptyTypes));
}
var body = Expression.Call(toString, method, searchValue);
return Expression.Lambda<Func<T, bool>>(body, parameterExp);
I don't know exactly how to initialize ExpressionCall. This is the thing which i want to know. Currently it is giving me error "MethodCallExpression does not contains constructor that takes 0 argument". I searched a lot but could not find any solution.