I am trying to create a dynamic query using expression trees to match the following expression:
var items = data.Where(i => i.CoverageType == 2).Select(i => i.LimitSelected);
I can create a where method and get the result from it; however, I cannot create a select method.
Here is my method:
var parm = Expression.Parameter(typeof(BaseClassData), "baseCoverage");
var queryData = data.AsQueryable();
var left = Expression.Property(parm, "CoverageType");
var right = Expression.Constant(2m);
var e1 = Expression.Equal(left, right);
var whereMethod = Expression.Call(
typeof(Queryable),
"Where",
new Type[] { queryData.ElementType },
queryData.Expression,
Expression.Lambda<Func<BaseClassData, bool>>(e1, new ParameterExpression[] { parm }));
This is what I use for the select method:
var selectParm = Expression.Property(parm, "LimitSelected");
var selectMethod = Expression.Call(
typeof(Enumerable),
"Select",
new Type[]{typeof(BaseClassData), typeof(decimal)},
whereMethod,
Expression.Lambda<Func<BaseClassData, decimal>>(selectParm, new ParameterExpression[]{ parm})
);
When I run the code, I get this error:
There is no generic Select method in the type System.Linq.Enumerable compatible with the arguments and arguments of the supplied type. No type arguments should be provided unless the method is generic.
I also tried changing Enumerable to Queryable and I get the same error.