查询应根据各种条件从查找表返回值。程序应在运行时使用元信息来构建这些表达式。 任何可能出现表达式的地方。
可能的表达式可以是:
DataRow test = lookup.Where(item => (item.Field<string>("K1_Filter") =="1100")).FirstOrDefault();
DataRow test = lookup.Where(item => (item.Field<string>("K1_Filter") =="1100" && item.Field<string>("K2_Filter") =="abc" )).FirstOrDefault();
DataRow test = lookup.Where(item => (item.Field<string>("K1_Filter").Contains("1100") && item.Field<string>("K2_Filter") =="abc" )).FirstOrDefault();
#
我需要为此查询构建一个表达式树:
using ExpressionLinq = System.Linq.Expressions;
IQueryable<DataRow> lookup = dataTable.AsEnumerable().AsQueryable();
DataRow test = lookup.Where(item => (item.Field<string>("K1_Filter") == "1100")).FirstOrDefault();
这就是我要做的:
ExpressionLinq.ParameterExpression pe = ExpressionLinq.Expression.Parameter(typeof(DataRow), "item");
ExpressionLinq.Expression right = ExpressionLinq.Expression.Constant("1100");
MethodInfo mi = typeof(DataRowExtensions).GetMethod("Field", new Type[] { typeof(DataRow), typeof(string) });
mi = mi.MakeGenericMethod(typeof(string));
List<ExpressionLinq.Expression> list = new List<ExpressionLinq.Expression>();
ExpressionLinq.ParameterExpression datarow = ExpressionLinq.Expression.Parameter(typeof(DataRow), "item");
ExpressionLinq.ConstantExpression constant = ExpressionLinq.Expression.Constant("K1_Filter", typeof(string));
list.Add(datarow);
list.Add(constant);
ReadOnlyCollection<ExpressionLinq.Expression> collection = new ReadOnlyCollection<ExpressionLinq.Expression>(list);
ExpressionLinq.MethodCallExpression methodCall =
ExpressionLinq.Expression.Call(null, mi, collection);
ExpressionLinq.Expression predicateBody = ExpressionLinq.Expression.Equal(methodCall, right);
ExpressionLinq.MethodCallExpression whereCallExpression =
ExpressionLinq.Expression.Call(
typeof(System.Linq.Queryable),
"Where",
new Type[] { lookup.ElementType },
lookup.Expression,
ExpressionLinq.Expression.Lambda<Func<DataRow, bool>>(predicateBody, new ExpressionLinq.ParameterExpression[] { pe }));
ExpressionLinq.MethodCallExpression firstordefault =
ExpressionLinq.Expression.Call(
typeof(Enumerable),
"FirstOrDefault",
new Type[] { lookup.ElementType },
whereCallExpression);
var results = lookup.Provider.CreateQuery<DataRow>(firstordefault);
最后一行返回以下错误消息:
System.ArgumentException:“参数表达式无效”
有人可以帮助我吗?我找不到错误。