• [织梦吧]唯一域名:www.dedecms8.com,织梦DedeCMS学习平台.

当前位置: > 编程与数据库 > net编程 >

动态构造Lambda表达式(2)

来源: www.dedecms8.com 编辑:织梦吧 时间:2012-06-27点击:
p => ((((True And (p.Name.ToString() = "JT")) And (p.Sex.ToString() = "男")) And (p.Age.ToString() = "20")) And (p.Birthday.ToString() = "02/02/2008"))
new ConstructLambda<Person>().GetOrLambdaExpression(dictionary).ToString()返
回结果:

p => ((((False Or (p.Name.ToString() = "JT")) Or (p.Sex.ToString() = "男")) Or (p.Age.ToString() = "20")) Or (p.Birthday.ToString() = "02/02/2008"))
//构造常量表达式
Expression expression_return = Expression.Constant(true);
//构造表达式参数  类似于p=> 中的p
ParameterExpression expression_param = Expression.Parameter(TType, "p");
//遍历所有关键词 生成查询条件
foreach (string key in dictionary.Keys)
{
//生成类似与“p.Name.ToString()==常量”的表达式
Expression temp = Expression.Equal(
//后面两行先用.Property 得到p.Name 然后用call得到p.Name.ToString();
Expression.Call(
Expression.Property(expression_param, TType.GetProperty(key)),TType.GetMethod("ToString")),
Expression.Constant(dictionary[key]));
//合并查询条件
expression_return = Expression.And(expression_return, temp);
}

 

About D8

  • ©2014 织梦吧(d8) DedeCMS学习交流平台
  • 唯一网址 www.DedeCMS8.com 网站地图
  • 联系我们 1978130638@qq.com ,  QQ