小编use*_*023的帖子

无法让 double.TryParse 在 Linq 表达式树中工作

我正在尝试使用 Linq 表达式为 IQueryable 数据源创建动态 where 子句。我无法让 TryParse 函数在其中一个表达式中工作。这是我想要做的:

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);
Run Code Online (Sandbox Code Playgroud)

一切正常,直到 …

c# linq reflection lambda expression-trees

5
推荐指数
1
解决办法
750
查看次数

标签 统计

c# ×1

expression-trees ×1

lambda ×1

linq ×1

reflection ×1