EF4.0、4.3创建表达式树状动态查询总结

---------------------------------------------快速适用

效果:

where name like '%王%' and Age>=35 or Age<10

构建表达式树:(快速式)

1.添加命名空间:

using System.Linq.Expressions;

2.声明参数表达式(比如Book实体)

 ParameterExpression param = Expression.Parameter(typeof(Book));

3.

Expression exp = Expression.Constant(true);初始化表达式树;

4.通过反射机制调用string.Contains,构建like;

var methodinfo = typeof(string).GetMethod("Contains");

5.拼接

//Expression.And:表示 and 后面的条件当然还有Or、 >=、<=、=等

//exp:and前面的语句,拼接之前的语句;

//Expression.Call():方法调用表达式;

//Expression.Property(param, "Name")字段或属性表达式

//methodinfo:调用上面Contains方法;

//Expression.Constant(s1):常量表达式;s1=“王”;

exp = Expression.And(exp, Expression.Call(Expression.Property(param, "Name"),methodinfo, Expression.Constant(s1)));

exp = Expression.And(exp, Expression.GreaterThanOrEqual(Expression.Property(param, "Id"),Expression.Constant(35)));

exp = Expression.Or(exp, Expression.LessThan(Expression.Property(param, "Id"), Expression.Constant(10)));

6.描述lambda表达式

var lam = Expression.Lambda>(exp, param);

7.调用

var lis = books.Where(lam).ToList();

 

------------------------------------------------------------详细(转述网友秦迷)

//---------------动态创建表达式目录树---------------//

//名字模糊查询tony

//年龄大于等于20

//年龄小于30

string uname = "tony";

List expList = new List(2);

ParameterExpression parExp = Expression.Parameter(typeof(UserInfo), "infos");//创建需要查找的参数

 

//查姓名infos.UserName.Contains("tony")

MemberExpression unameExp = Expression.Property(parExp, "UserName");

MethodInfo containsMethod = typeof(string).GetMethod("Contains");//using System.Reflection;指定实现Contains方法

ConstantExpression unamevalueExp = Expression.Constant(uname, typeof(string));

MethodCallExpression containsExp = Expression.Call(unameExp, containsMethod, unamevalueExp);

expList.Add(containsExp);

 

//查年龄infos.Age>=20

MemberExpression age1 = Expression.Property(parExp, "Age");

ConstantExpression agevalue1 = Expression.Constant(20, typeof(int));

BinaryExpression gteExp = Expression.GreaterThanOrEqual(age1, agevalue1);

expList.Add(gteExp);

//查年龄infos.Age<30

//if (1 > 2)

//{

MemberExpression age2 = Expression.Property(parExp, "Age");

ConstantExpression agevalue2 = Expression.Constant(30, typeof(int));

BinaryExpression ltExp = Expression.LessThan(age2, agevalue2);

expList.Add(ltExp);

//}

 

 

//把上面的表达式连接起来

Expression whereExp = null;

foreach (var item in expList)

    {

        if (whereExp == null)

                {

                    whereExp = item;

                }

                else

                {

                    whereExp = Expression.And(whereExp, item);

                    /*

                     * 出错信息:

                     * 没有为类型“System.Boolean”和“System.Boolean”定义二进制运算符 Add。

                     * 是And,开始我写错成了Add一直找不到原因

                     */

                }

            }

//生成Lambda表达式

//Expression> lambda = Expression.Lambda>(whereExp, parExp);

LambdaExpression lambda = Expression.Lambda>(whereExp, parExp);

Console.WriteLine(lambda);

/*

* 生成结果:

* infos => ((infos.UserName.Contains("tony") And (infos.Age>=20)) And (infos.Age<30))

*/

转载于:https://www.cnblogs.com/guozefeng/p/3231491.html

你可能感兴趣的:(EF4.0、4.3创建表达式树状动态查询总结)