日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

为给定的Lambda表达式构建表达式树

發布時間:2024/1/18 编程问答 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 为给定的Lambda表达式构建表达式树 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

這是用于將給定的LINQ表達式轉換為對應的表達式樹的代碼示例。

//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?}?}?}; //語句:對于給定的數據源,如果我們要查找主持了DinnerID 1的主機,

//對應的Lambda表達式為

// DinnerList.Where(host => host.DinnerID == 1 && host.Dinner.Eventdate <= DateTime.Now).OrderBy(host => host.HostName);

//這是創建此Lambda表達式的表達式樹的逐步說明

//將集合轉換為Queryable對象,以便調用IQueryable擴展方法。

//作為方法“ Where”和“ orderBy”僅可在Qeryable對象上調用。

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

總結

以上是生活随笔為你收集整理的为给定的Lambda表达式构建表达式树的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。