动态创建Lambda表达式树用于查询,创建成功但查询时报错,在线急求高手解答,谢谢!
以下是创建表达式树,生成如:f=>f.ID==1
ParameterExpression expParameter =Expression.Parameter(typeof(TEntity), "f"); ParameterExpression expl = Expression.Parameter(pType,"f." + fieldInfo.Name); ConstantExpression expr = Expression.Constant(convertedValue, pType); Expression expBody = null; expBody = Expression.Equal(expl, expr); Expression<Func<TEntity, bool>> lamExp = Expression.Lambda<Func<TEntity, bool>>(expBody, expParameter);
但真正查询时却报错:
The parameter 'f.ID' was not bound in the specified LINQ to Entities query expression.
经查网上,有人说原因是参数GUID不同所导致的,需要用到ExpressionVisitor来替换参数,我不明白如何操作,还请知道的人告之,谢谢!