Convert Linq to Sql Expression to Expression Tree?

This should do it: var query = dc.Regions.AsQueryable(); ParameterExpression pe = Expression. Parameter(typeof(Region), "region"); Expression id = Expression. PropertyOrField(pe, "RegionID"); Expression two = Expression.

Constant(2); Expression e1 = Expression. GreaterThan(id, two); Expression description = Expression. PropertyOrField(pe, "RegionDescription"); MethodInfo method = typeof(string).

GetMethod("Contains", new {typeof(string)}); Expression ern = Expression. Constant("ern",typeof(string)); Expression e2 = Expression. Call(description, method, ern); Expression e3 = Expression.

And(e1, e2); MethodCallExpression whereCallExpression = Expression. Call( typeof(Queryable), "Where", new Type { query. ElementType }, query.

Expression, Expression. Lambda>(e3, new ParameterExpression { pe })); var results = query.Provider. CreateQuery(whereCallExpression); List lst = results.ToList() And to select just RegionID from the result set do the following: MethodCallExpression selectExpression = Expression.

Call( typeof(Queryable), "Select", new{ typeof(Region), typeof(int)}, whereCallExpression, Expression. Lambda>(id, pe)); var regionIDsQuery = query.Provider. CreateQuery(selectExpression); List regionIDs = regionIDsQuery.ToList().

This should do it: var query = dc.Regions.AsQueryable(); ParameterExpression pe = Expression. Parameter(typeof(Region), "region"); Expression id = Expression. PropertyOrField(pe, "RegionID"); Expression two = Expression.

Constant(2); Expression e1 = Expression. GreaterThan(id, two); Expression description = Expression. PropertyOrField(pe, "RegionDescription"); MethodInfo method = typeof(string).

GetMethod("Contains", new {typeof(string)}); Expression ern = Expression. Constant("ern",typeof(string)); Expression e2 = Expression. Call(description, method, ern); Expression e3 = Expression.

And(e1, e2); MethodCallExpression whereCallExpression = Expression. Call( typeof(Queryable), "Where", new Type { query. ElementType }, query.

Expression, Expression. Lambda>(e3, new ParameterExpression { pe })); var results = query.Provider. CreateQuery(whereCallExpression); List lst = results.ToList(); And to select just RegionID from the result set do the following: MethodCallExpression selectExpression = Expression.

Call( typeof(Queryable), "Select", new{ typeof(Region), typeof(int)}, whereCallExpression, Expression. Lambda>(id, pe)); var regionIDsQuery = query.Provider. CreateQuery(selectExpression); List regionIDs = regionIDsQuery.ToList().

Thanks Alot. It's very useful. Can you introduce some article about Expression Tree?

How you being master in Expression Tree? Thanks – Nima Mar 8 at 4:51 Please edit your post and replace "widget" with "ern". Can you show me what should I do if I want just select "RegionID".

Thanks – Nima Mar 8 at 5:26 @Nima Glad I could help. Thanks for saying that but I'm no master of Expression Trees. They're just an interesting structure that I work with to gain a better understanding of Linq in general.

I recommend reading the documentation of course, but also writing some standard Linq queries and then examining their Expression properties. Little by little this will help you understand the underlying mechanics. I'll update my answer to include the Select call.

– Sorax Mar 8 at 15:21 @Nima Here's a pretty good example article to get you started msdn.microsoft. Com/en-us/library/bb882637.aspx. I also recommend downloading LinqPad if you haven't already.It really speeds up the learning process by making experimentation so easy.

Good Luck! – Sorax Mar 8 at 15:33 I write your select call statment for "Order" table but I can't select my desire column such as: OrderID,OrderDate,ShipAddress,RequiredDate. May you help me please?

Thanks – Nima Mar 87 at 8:36.

ParameterExpression pe = Expression. Expression id = Expression. Expression two = Expression.

Expression e1 = Expression. Expression description = Expression. MethodInfo method = typeof(string).

Expression ern = Expression. Expression e2 = Expression. Expression e3 = Expression.

MethodCallExpression whereCallExpression = Expression. New Type { query. Var results = query.Provider.

MethodCallExpression selectExpression = Expression. Var regionIDsQuery = query.Provider.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions