50字范文,内容丰富有趣,生活中的好帮手!
50字范文 > 为给定的Lambda表达式构建表达式树

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

时间:2023-02-23 18:32:24

相关推荐

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

这是用于将给定的LINQ表达式转换为对应的表达式树的代码示例。

//usingLINQAlias=System.Linq.Expressions;List<Host>dinnerList=newList<Host>(){newHost{DinnerID=4,HostName="Abc",HostContactDetails="123456789",Dinner=newDinner(){DinnerID=4,Discription="Description1",Eventdate=newDateTime(DateTime.Now.Year,DateTime.Now.Month-1,DateTime.Now.Day-3)}},newHost{DinnerID=1,HostName="Xyz",HostContactDetails="987654321",Dinner=newDinner(){DinnerID=1,Discription="Description2",Eventdate=newDateTime(DateTime.Now.Year,DateTime.Now.Month-2,DateTime.Now.Day-1)}},newHost{DinnerID=1,HostName="Abc",HostContactDetails="7845915356",Dinner=newDinner(){DinnerID=1,Discription="Description3",Eventdate=newDateTime(DateTime.Now.Year,DateTime.Now.Month-1,DateTime.Now.Day-2)}},newHost{DinnerID=1,HostName="Pqr",HostContactDetails="9475815364",Dinner=newDinner(){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>();//Step1:Declaretheparameter'host'//So,Buildtheparameterexpressionfor'host'asLINQAlias.ParameterExpressionhostParameterExpression=Expression.Parameter(typeof(Host),"host");//Step2:UsethisParametertobuildtheexpressionforthehost.DinnerID==1//L.H.SexpressionistheproperetyoftheobjectHosthenceneedtobuildtheexpressionasExpressionleft=Expression.Property(hostParameterExpression,typeof(Host).GetProperty("DinnerID",typeof(int)));Expressionright=Expression.Constant(1);ExpressionpredicateExpression=Expression.Equal(left,right);//Step3:Buildanexpressionforhost.Dinner.Eventdate<=DateTime.NowExpressionanotherLeft=Expression.Property(hostParameterExpression,typeof(Host).GetProperty("Dinner",typeof(Dinner)));TypetypeDinner=anotherLeft.Type;ExpressioneventDateofDinner=Expression.Property(anotherLeft,typeDinner.GetProperty("Eventdate",typeof(DateTime)));ExpressionanotherRight=Expression.Constant(DateTime.Now);ExpressionanotherPredicateExpression=Expression.LessThanOrEqual(eventDateofDinner,anotherRight);//Step4:Buildatreeforhost=>host.DinnerID==1&&host.Dinner.Eventdate<=DateTime.NowfromthesubExpressionspredicateExpressionandanotherPredicateExpressionExpressionpredicateBody=Expression.AndAlso(predicateExpression,anotherPredicateExpression);//Step5:CallWhereontheQueryableDataSourcebypassingthisexpressiontothecall//ExpressionTreefordinnerList3.Where(host=>host.DinnerID==3&&host.Dinner.Eventdate<=DateTime.Now)MethodCallExpressionwhereCallExpression=Expression.Call(typeof(Queryable),"Where",newType[]{HostList.ElementType},HostList.Expression,Expression.Lambda<Func<Host,bool>>(predicateBody,newParameterExpression[]{hostParameterExpression}));//Step6:Buildtheexpressionforhost.HostNameExpressionhostNamePredicateBody=Expression.Property(hostParameterExpression,typeof(Host).GetProperty("HostName",typeof(string)));//Step7:Extendthetreeforthecalltoorderby//.OrderBy(host=>host.HostName)MethodCallExpressionorderByExpression=Expression.Call(typeof(Queryable),"OrderBy",newType[]{HostList.ElementType,typeof(string)},//typeofinargumentstotheOrderByqueryableMethodwhereCallExpression,Expression.Lambda<Func<Host,string>>(hostNamePredicateBody,newParameterExpression[]{hostParameterExpression})//LastParametertotheCallrepresentsthecontainerLambdaexpressionthatholdstheexpressionbuiltinthecall.Fore.g.herethefinalexpressiongoingtogetbuiltiswhereexpression.OrderBy(host=>host.name).Intheexpressionhostistheinargumenttothemethodandstringistheoutargument.Sothecontainerexpressionimpliesthesame.ItcreatesthedynamicmethodwiththereturnTypeandsignatureas"<<ruturnType>>Func(<<inArgument>>)-->stringfunc(host)".//Animmediatenextargumenttothemethodisthebodyofthemethodwhichishost.HostNamerepresentedbyanexpressionhostNamePredicateBodyandanargumenthostParameterExpressiontendstotheHostwhichisanin-argumnettotheFuncMethod.);//Step9:CreateanexecutableQueryIQueryable<Host>result=HostList.Provider.CreateQuery<Host>(orderByExpression);//Step10:ExecutetheQueryandDisplaytheresult.foreach(Hosthostinresult)MessageBox.Show(host.HostName);

From: /topic/net/insights/940724-building-expression-tree-given-lambda-expression

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。