为给定的Lambda表达式构建表达式树
生活随笔
收集整理的這篇文章主要介紹了
为给定的Lambda表达式构建表达式树
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
這是用于將給定的LINQ表達(dá)式轉(zhuǎn)換為對應(yīng)的表達(dá)式樹的代碼示例。
//using?LINQAlias?=?System.Linq.Expressions;?List<Host>?dinnerList?=?new?List<Host>()?{?new?Host?{?DinnerID?=?4,?HostName?=?"Abc",?HostContactDetails?=?"123456789",?Dinner?=?new?Dinner()?{?DinnerID?=?4,?Discription?=?"Description1",?Eventdate?=?new?DateTime(DateTime.Now.Year,?DateTime.Now.Month?-?1,?DateTime.Now.Day?-?3)?}?},?new?Host?{?DinnerID?=?1,?HostName?=?"Xyz",?HostContactDetails?=?"987654321",?Dinner?=?new?Dinner()?{?DinnerID?=?1,?Discription?=?"Description2",?Eventdate?=?new?DateTime(DateTime.Now.Year,?DateTime.Now.Month?-?2,?DateTime.Now.Day?-?1)?}?},?new?Host?{?DinnerID?=?1,?HostName?=?"Abc",?HostContactDetails?=?"7845915356",?Dinner?=?new?Dinner()?{?DinnerID?=?1,?Discription?=?"Description3",?Eventdate?=?new?DateTime(DateTime.Now.Year,?DateTime.Now.Month?-?1,?DateTime.Now.Day?-?2)?}?},?new?Host?{?DinnerID?=?1,?HostName?=?"Pqr",?HostContactDetails?=?"9475815364",?Dinner?=?new?Dinner()?{?DinnerID?=?1,?Discription?=?"Description4",?Eventdate?=?DateTime.Now?}?}?}; //語句:對于給定的數(shù)據(jù)源,如果我們要查找主持了DinnerID 1的主機(jī),//對應(yīng)的Lambda表達(dá)式為
// DinnerList.Where(host => host.DinnerID == 1 && host.Dinner.Eventdate <= DateTime.Now).OrderBy(host => host.HostName);
//這是創(chuàng)建此Lambda表達(dá)式的表達(dá)式樹的逐步說明
//將集合轉(zhuǎn)換為Queryable對象,以便調(diào)用IQueryable擴(kuò)展方法。
//作為方法“ Where”和“ orderBy”僅可在Qeryable對象上調(diào)用。
IQueryable<Host>?HostList?=?dinnerList.AsQueryable<Host>();?//Step?1?:?Declare?the?parameter?'host'//?So,?Build?the?parameter?expression?for?'host'?as?LINQAlias.ParameterExpression?hostParameterExpression?=?Expression.Parameter(typeof(Host),?"host");?//?Step?2?:?Use?this?Parameter?to?build?the?expression?for?the??host.DinnerID?==?1//?L.H.S?expression?is?the?properety?of?the?object?Host?hence?need?to?build?the?expression?as?Expression?left?=?Expression.Property(hostParameterExpression,?typeof(Host).GetProperty("DinnerID",?typeof(int)));Expression?right?=?Expression.Constant(1);?Expression?predicateExpression?=?Expression.Equal(left,?right);?//Step?3?:?Build?an?expression?for?host.Dinner.Eventdate?<=?DateTime.Now?Expression?anotherLeft?=?Expression.Property(hostParameterExpression,?typeof(Host).GetProperty("Dinner",?typeof(Dinner)));Type?typeDinner?=?anotherLeft.Type;Expression?eventDateofDinner?=?Expression.Property(anotherLeft,?typeDinner.GetProperty("Eventdate",?typeof(DateTime)));?Expression?anotherRight?=?Expression.Constant(DateTime.Now);?Expression?anotherPredicateExpression?=?Expression.LessThanOrEqual(eventDateofDinner,?anotherRight);?//Step?4:?Build?a?tree?for?host?=>?host.DinnerID?==?1?&&?host.Dinner.Eventdate?<=?DateTime.Now?from?the?subExpressions?predicateExpression?and?anotherPredicateExpression?Expression?predicateBody?=?Expression.AndAlso(predicateExpression,?anotherPredicateExpression);?//Step?5?:Call?Where?on?the?Queryable?DataSource?by?passing?this?expression?to?the?call//?Expression?Tree?for?dinnerList3.Where(host?=>?host.DinnerID?==?3?&&?host.Dinner.Eventdate?<=?DateTime.Now)?MethodCallExpression?whereCallExpression?=?Expression.Call(typeof(Queryable),"Where",new?Type[]?{?HostList.ElementType},HostList.Expression,Expression.Lambda<Func<Host,?bool>>(predicateBody,?new?ParameterExpression[]?{?hostParameterExpression?}));??//Step?6:Build?the?expression?for?host.HostName?Expression?hostNamePredicateBody?=?Expression.Property(hostParameterExpression,?typeof(Host).GetProperty("HostName",?typeof(string)));?//Step?7:?Extend?the?tree?for?the?call?to?order?by//.OrderBy(host?=>?host.HostName)?MethodCallExpression?orderByExpression?=?Expression.Call(typeof(Queryable),"OrderBy",new?Type[]?{?HostList.ElementType,?typeof(string)?},?//?type?of?in?arguments?to?the?OrderBy?queryable?MethodwhereCallExpression,Expression.Lambda<Func<Host,?string>>(hostNamePredicateBody,?new?ParameterExpression[]?{?hostParameterExpression?})?//?Last?Parameter?to?the?Call?represents?the?container?Lambda?expression?that?holds?the?expression?builtin?the?call.?For?e.g.?here?the?final?expression?going???????????????????to?get?built?is?whereexpression.OrderBy(host?=>?host.name).In?the?expression?host?is?the?in?argument?to?the?method?and?????????????????????string?is?the?out?argument.So?the?container?expression?implies?the?same?.?It?creates?the?dynamic?method?with?the?????????????????????????return?Type?and?signature?as?"<<ruturnType>>?Func(<<inArgument>>)?-->?string?func(host)".?//?An?immediate?next?argument?to?the?method?is?the?body?of?the?method?which?is?host.HostName?represented?by?an?expression????????????????????hostNamePredicateBody?and?an?argument?hostParameterExpression?tends?to?the?Host?which?is?an?in-argumnet?to?the?Func?Method.?);??//?Step?9?:Create?an?executable?Query?IQueryable<Host>?result??=?HostList.Provider.CreateQuery<Host>(orderByExpression);?//Step?10?:?Execute?the?Query?and?Display?the?result.foreach?(Host?host?in?result)MessageBox.Show(host.HostName);From: https://bytes.com/topic/net/insights/940724-building-expression-tree-given-lambda-expression
總結(jié)
以上是生活随笔為你收集整理的为给定的Lambda表达式构建表达式树的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 顺利找到工作了!
- 下一篇: linux下oracle错误日志,Lin