0

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?

Felype
  • 3,087
  • 2
  • 25
  • 36
  • the expression-tree API *does* support this, but the C# code->expression compiler: does not (at least, not as-of C# 9); there's no easy workaround here unless you want to write expression trees manually; and no, you can't trivially turn a delegate into an expression (although you can do the opposite, turning an expression into a delegate) – Marc Gravell Jun 16 '21 at 17:58
  • Are you trying to invent another [linq2db](https://github.com/linq2db/linq2db)? – Svyatoslav Danyliv Jun 16 '21 at 18:56
  • @SvyatoslavDanyliv yes, I've been working on this project since 2015 for my personal fun, use and learning. It's used solely by me and my own applications, no other people work on that lib or those applications. It started as a simple wrapper around AdoNet and it evolved into a more elaborated micro-orm – Felype Jun 17 '21 at 03:24

0 Answers0