Using a member access lambda expression to parametrise a LINQ to SQL predicate?

First, you need to change your method signature: private static IQueryable RestrictByProp(IQueryable query, Expression> selector) That will mean your lambda expression is converted into an expression tree instead of a delegate You'll then need to build an Expression>(predicate, lambda. Parameters); return query. Where(lambdaPredicate).

First, you need to change your method signature: private static IQueryable RestrictByProp(IQueryable query, Expression> selector) That will mean your lambda expression is converted into an expression tree instead of a delegate. You'll then need to build an Expression> from the existing expression tree. It will look something like this: LambdaExpression lambda = (LambdaExpression) selector; var predicate = Expression.

Equal(selector, Expression. Constant(1)); var lambdaPredicate = Expression. Lambda>(predicate, lambda.

Parameters); return query. Where(lambdaPredicate).

Thanks. I'm trying to apply that to my beast of a query now. I suppose I'll have to build up a massive expression tree.

Or is this where ExpressionVisitor comes in handy? For anyone else who needs to use this code, I needed to change something to make it work (and I can't edit :): 1) Expression. Equal(lambda*.

Body* ... and 2) Expression. Lambda> – stucampbell Aug 9 '10 at 16:24 @stucampbell: Thanks, I've fixed the typos. Not sure about ExpressionVisitor - I haven't used it myself yet.

– Jon Skeet Aug 9 '10 at 16:29.

(Here I'm just adding a simple 'where' clause - in my real code I'd be using the lambda to pick which property to use for a join). LINQ to SQL is also happy if you pass in an Expression> (i.e. When the parameter is x=>x.ID == 1) but that defeats the object because I need the value of the right-hand operand to be determined within the query.

Is there a way to somehow munge the lambda expression in RestrictByProp() so that LINQ to SQL knows how to generate the SQL?

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