=35orAge<10構建表達式樹:(快速式)1.添加命名空間:usingSystem.Linq.Expressions;2.聲明參數表達式(比如Book實體)ParameterExpressionparam=Expression.Parameter(typeof(Book));3.Expressione" />

日韩久久久精品,亚洲精品久久久久久久久久久,亚洲欧美一区二区三区国产精品 ,一区二区福利

EF4.0、4.3創建表達式樹狀動態查詢總結

系統 1714 0

---------------------------------------------快速適用

效果:

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<Func<Book, bool>>(exp, param);

7.調用

var lis = books.Where<Book>(lam).ToList();

?

------------------------------------------------------------詳細(轉述網友秦迷)

//---------------動態創建表達式目錄樹---------------//

//名字模糊查詢tony

//年齡大于等于20

//年齡小于30

string uname = "tony";

List<Expression> expList = new List<Expression>(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<Func<UserInfo, bool>> lambda = Expression.Lambda<Func<UserInfo, bool>>(whereExp, parExp);

LambdaExpression lambda = Expression.Lambda<Func<UserInfo, bool>>(whereExp, parExp);

Console.WriteLine(lambda);

/*

* 生成結果:

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

*/

EF4.0、4.3創建表達式樹狀動態查詢總結


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 井冈山市| 安康市| 辽宁省| 静安区| 浦江县| 永州市| 武穴市| 上犹县| 策勒县| 连州市| 高阳县| 梁河县| 龙南县| 永平县| 三门县| 军事| 井冈山市| 商南县| 中西区| 莱芜市| 榆中县| 吉安市| 平原县| 望城县| 温宿县| 鲁甸县| 嘉荫县| 南溪县| 柞水县| 北碚区| 大新县| 罗定市| 崇明县| 北宁市| 长春市| 玛多县| 嘉峪关市| 腾冲县| 冕宁县| 伊春市| 西昌市|