数据集中的 LINQ 表达式
我在条件数据集中传递表达式时遇到问题..我正在粘贴我的所有代码..如果我错了,请纠正我,并建议是否有简短的方法..
代码是检查(年龄> 10)&& (性别=='M'),我需要在数据集中应用此规则。
Expression<Func<int, bool>> ageexpr; // = creteriaattributeIDtest => creteriaattributeIDtest < Convert.ToInt32(rightval1);
ParameterExpression numparam = Expression.Parameter(typeof(int), "age");
ConstantExpression criteriaValue1 = Expression.Constant(Convert.ToInt32(rightval1), typeof(int));
BinaryExpression comparison1 = Expression.LessThan(numparam, criteriaValue1);
ageexpr = Expression.Lambda<Func<int, bool>>(
comparison1,
new ParameterExpression[] { numparam });
Func<int, bool> resultage = ageexpr.Compile();
// Invoking the delegate and writing the result to the console.
bool firstrule = resultage(14);
Console.WriteLine("1st Rule" + firstrule);
// DataView res1 = dt1.AsEnumerable().Where(resultage(Convert.ToInt32(rightval1))).AsDataView();
Expression<Func<string, bool>> genexpr;
ParameterExpression genparam = Expression.Parameter(typeof(string), "gender");
ConstantExpression criteriaValue2 = Expression.Constant("M", typeof(string));
BinaryExpression comparison2 = Expression.Equal(genparam, criteriaValue2);
genexpr = Expression.Lambda<Func<string, bool>>(
comparison2,
new ParameterExpression[] { genparam });
Func<string, bool> resultgen = genexpr.Compile();
bool secondrule = resultgen("M");
// Invoking the delegate and writing the result to the console.
Console.WriteLine("2nd Rule" + secondrule);
Expression finexpr = Expression.AndAlso(Expression.Constant(firstrule), Expression.Constant(secondrule));
Console.WriteLine(Expression.Lambda<Func<bool>>(finexpr).Compile()());
i am getting problems while passing expression in dataset where condition.. I am pasting all my codes .. please correct me if i am wrong and suggest if there are short methods..
code is to check (age>10)&&(gender=='M'), i need to apply this rule in dataset.
Expression<Func<int, bool>> ageexpr; // = creteriaattributeIDtest => creteriaattributeIDtest < Convert.ToInt32(rightval1);
ParameterExpression numparam = Expression.Parameter(typeof(int), "age");
ConstantExpression criteriaValue1 = Expression.Constant(Convert.ToInt32(rightval1), typeof(int));
BinaryExpression comparison1 = Expression.LessThan(numparam, criteriaValue1);
ageexpr = Expression.Lambda<Func<int, bool>>(
comparison1,
new ParameterExpression[] { numparam });
Func<int, bool> resultage = ageexpr.Compile();
// Invoking the delegate and writing the result to the console.
bool firstrule = resultage(14);
Console.WriteLine("1st Rule" + firstrule);
// DataView res1 = dt1.AsEnumerable().Where(resultage(Convert.ToInt32(rightval1))).AsDataView();
Expression<Func<string, bool>> genexpr;
ParameterExpression genparam = Expression.Parameter(typeof(string), "gender");
ConstantExpression criteriaValue2 = Expression.Constant("M", typeof(string));
BinaryExpression comparison2 = Expression.Equal(genparam, criteriaValue2);
genexpr = Expression.Lambda<Func<string, bool>>(
comparison2,
new ParameterExpression[] { genparam });
Func<string, bool> resultgen = genexpr.Compile();
bool secondrule = resultgen("M");
// Invoking the delegate and writing the result to the console.
Console.WriteLine("2nd Rule" + secondrule);
Expression finexpr = Expression.AndAlso(Expression.Constant(firstrule), Expression.Constant(secondrule));
Console.WriteLine(Expression.Lambda<Func<bool>>(finexpr).Compile()());
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不太清楚你的问题到底是什么,但如果你只想通过 LINQ 创建一个 DataView,为什么不使用这样的东西:
如果你真的想使用表达式,试试这个:
你必须将参数传递给你的委托。
Not quite clear what your problem actually is, but if you just want to create a DataView via LINQ, why not use something like this:
If you really want to use Expressions, try this:
You have to pass a parameter to your delegates.