vendredi 10 juillet 2015

Unable to get double.TryParse to work in Linq Expression Tree

I am trying to create a dynamic where clause using Linq Expressions for an IQueryable data source. I can't get the TryParse function to work in one of the Expressions. Here is what I am trying to do:

IQueryable<trial_global> globalTrials = _trialsRepository.GlobalDataFiltered(filterId).AsQueryable();

BindingFlags bindingFlags = BindingFlags.Public | BindingFlags.Static;
MethodInfo tryParseMethod = typeof(double).GetMethod("TryParse", bindingFlags, null, new Type[] { typeof(string), typeof(double).MakeByRefType() }, null);
Expression tempN = Expression.Parameter(typeof(double), "tempN");
Expression left = Expression.Call(tryParseMethod, new[] { metricReference, tempN });

Expression right = Expression.Constant(true);
Expression predicateBody = Expression.Equal(left, right);

MethodCallExpression whereCallExpression = Expression.Call(
                            typeof(Queryable),
                            "Where",
                            new Type[] { globalTrials.ElementType },
                            globalTrials.Expression,
                            Expression.Lambda<Func<trial_global, bool>>(predicateBody, new ParameterExpression[] { pe })
                            );

var results = globalTrials.Provider.CreateQuery<trial_global>(whereCallExpression);

Everything works fine until results gets assigned where I get the following error:

variable 'tempN' of type 'System.Double' referenced from scope '', but it is not defined

What am I missing here? I suspect it has to do with the 2nd parameter in the double.TryParse function being an out parameter.





Aucun commentaire:

Enregistrer un commentaire