Peter Riesz
Peter Riesz

Reputation: 3396

Is it possible to reference a ParameterExpression inside Linq query syntax?

Here is a simplified example of what I am trying to achieve, I'm not sure if it is even possible or if I am going about this all wrong.

Expression<T, bool> UseDataContext<T>(Expression<Func<DataContext, T, bool>> expr, Expression dataContextExpr){
    // use ExpressionVisitor and return new LambaExpression
}    

Expression<Func<DataContext, Foo, bool>> fooFilterExpr = (db, foo) 
    => db.FilteredFoos.Any(vf => vf.FooId == foo.Id);

Expression<Func<DataContext, Bar, bool>> barExpr = (db) => (
    from bar in db.Bars
    join foo in db.Foos.Where(UseDataContext(fooFilterExpr, ?db Expression?))
    select bar
);

Upvotes: 0

Views: 125

Answers (1)

Marc Gravell
Marc Gravell

Reputation: 1062660

Not directly, basically. That isn't something that is available in the syntax. You'd have to create all the lambdas manually for that.

Suggestion: take a simpified but working example, and run it through https://sharplab.io/ specifying C# as the "Results" - that will give you the manual code to do that, including the ParameterExpression, which you can then use to replace it with a manually written version; example. Then replace the bits that you need to.

I never said it would be pretty...

Upvotes: 1

Related Questions