【发布时间】:2017-07-04 15:56:59
【问题描述】:
我一直在编写表达式树来动态创建 lambda 表达式。
从小处着手,我将 JSON 文件中的数据读取到 Class 对象中。并尝试在两个条件下构建 where 条件。
string jsonnew = File.ReadAllText(@"C:\Users\nested_Json.txt");
var rootObject1 = JsonConvert.DeserializeObject<Citizen>(jsonnew);
IEnumerable<Citizen> enumerable = new[] { rootObject1 };
IQueryable<Citizen> queryableDataaa = enumerable.AsQueryable();
ParameterExpression pe1 = Expression.Parameter(typeof(string), "rootobject");
PropertyInfo field = typeof(Citizen).GetProperty("name");
PropertyInfo field2 = typeof(Citizen).GetProperty("id");
ParameterExpression targetExp = Expression.Parameter(typeof(Citizen), "rootobject");
ParameterExpression valueExp = Expression.Parameter(typeof(string), "\"StrozeR\"");
ParameterExpression valueExp2 = Expression.Parameter(typeof(Int32),"1765116");
MemberExpression fieldExp = Expression.Property(targetExp, field);
MemberExpression fieldExp2 = Expression.Property(targetExp, field2);
Expression assignExp = Expression.Equal(fieldExp, valueExp);
Expression assignExp2 = Expression.Equal(fieldExp2, valueExp2);
Expression predicateBody1 = Expression.AndAlso(assignExp, assignExp2);
MethodCallExpression whereCallExpression1 = Expression.Call(
typeof(Queryable),
"where",
new Type[] { queryableDataaa.ElementType},
queryableDataaa.Expression,
Expression.Lambda<Func<string, bool>>(predicateBody1, new ParameterExpression[] { pe1 }));
任何人都可以帮助我了解我为什么会得到
类型“System.Linq.Queryable”上没有通用方法“where”与提供的类型参数和参数兼容。如果方法是非泛型的,则不应提供类型参数。错误
【问题讨论】:
标签: c# .net json expression-trees