Generate dynamic UPDATE command from Expression<Func<T, T>>

Posted by Rui Jarimba on Stack Overflow See other posts from Stack Overflow or by Rui Jarimba
Published on 2012-03-16T15:50:01Z Indexed on 2012/03/23 17:29 UTC
Read the original article Hit count: 249

Filed under:
|
|

I'm trying to generate an UPDATE command based on Expression trees (for a batch update).

Assuming the following UPDATE command:

UPDATE Product
SET ProductTypeId = 123,
ProcessAttempts = ProcessAttempts + 1

For an expression like this:

Expression<Func<Product, Product>> updateExpression = entity =>
    new Product() {
        ProductTypeId = 123,
        ProcessAttempts = entity.ProcessAttempts + 1
    };

How can I generate the SET part of the command?

SET ProductTypeId = 123,
ProcessAttempts = ProcessAttempts + 1

© Stack Overflow or respective owner

Related posts about sql

Related posts about LINQ