I intend to write a function that receives a bunch of expressions and returns an object with an SQL QueryString and a dictionary with parameters, this function is specifically intended to create update statements, i have 2 other functions able to update 1 or a bunch of objects, but it sends ALL fields and values of the instances and I need something to update the database according to a Predicate, not objects that I have loaded on the application.
The signature for this function would be something like this:
public QueryAndParams GenerateUpdateQuery<T>(Expression<Action<T>> Set, Expression<Func<T, bool>> Where)
The usage:
var query = sqlGenerator.GenerateUpdateQuery<Schedules>(
Set = x=> {
x.ParentSchedule = null;
x.SomeOtherFieldJustForExample = false;
},
Where = x=> x.ParentSchedule == someOtherSchedule.RID
);
My question here is that with the second expression (for Where Clause), it works fine, I'm able to dissect the expression and turn it into a working where clause, but the Expression for Set gives me compiler error **CS0832**: An expression tree may not contain an assignment operator.
and **CS0834**: A lambda expression must have an expression body to be converted to an expression tree.
on the user-code
Is there a way to have a function receive something that looks like an Action<T>
, but inside is an Expression Tree, or is there a way to turn Action<T>
to an expression tree somehow, in a way that I don't get these compiler limitations on User-code?