简体   繁体   中英

How to force SQL parameter instead of constant in Expression predicate

I want to create predicate expression so that it translates into WHERE clause with SQL parameter, eg:

WHERE e.Id = @id_1;

I have this code

int id = 1;
Expression<Func<Entity, int>> keySelector = e => e.Id;
BinaryExpression equals = Expression.Equal(
   keySelector.Body, 
   Expression.Constant(id, keySelector.Body.Type)); //replace this with a variable

var predicate = Expression.Lambda<Func<Entity, bool>>(equals, keySelector.Parameters[0]);

but it translates into:

WHERE e.Id = 1;

which generates new query execution plan for each id.

Does EF core provide some internal mechanism, that I could (should) use, eg special subclass of Expression, or ExpressionVisitor?

Does EF core provide some internal mechanism, that I could (should) use, eg special subclass of Expression, or ExpressionVisitor?

Nothing special. All you need is to emulate C# closure. This could be done in several ways - using anonymous type, concrete type, Tuple or even real C# compiler generated closure like

int id = 1;
Expression<Func<int>> valueSelector = () => id;
var value = valueSelector.Body; // use this in place of Expression.Constant

Example of anonymous type approach

int id = 1;
var variable = new { id };
var value = Expression.Property(Expression.Constant(variable), nameof(id));

Similar with new Tuple<int>(id) and Item1 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM