Generate dynamic UPDATE command from the expression <Func <T, T >>

I am trying to create 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:

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

How can I generate a SET command in a command?

 SET ProductTypeId = 123, ProcessAttempts = ProcessAttempts + 1 
+4
source share
1 answer

This is a very simplified approach, but I hope it is good enough:

 private static string ConvertToSetCommand<T>(Expression<Func<T, T>> exp) { if (exp.Body.NodeType != ExpressionType.MemberInit) { throw new ArgumentException("The expression must have an object initializer.", "exp"); } var builder = new StringBuilder("SET ", 100); exp = (Expression<Func<T, T>>) new OmitParametersVisitor().Visit(exp); var memberInit = (MemberInitExpression) exp.Body; foreach (var assignment in memberInit.Bindings) { builder.Append(assignment.ToString()); builder.Append(", "); } builder.Length -= 2; // Remove the last comma return builder.ToString(); } private class OmitParametersVisitor : ExpressionVisitor { protected override Expression VisitMember(MemberExpression node) { if (node.Expression != null && node.Expression.NodeType == ExpressionType.Parameter) { return Expression.Parameter(node.Type, node.Member.Name); } else { return base.VisitMember(node); } } } 
+2
source

Source: https://habr.com/ru/post/1401851/


All Articles