public static Expression PrepareExpressionFromAnonymous <TSource>(Expression sourceExpression, Expression <Func <TSource, object> > expression)
        {
            if (expression == null)
            {
                throw new ArgumentNullException(nameof(expression));
            }

            // Anonymous initializations are not implemented as member initialization but as plain constructor call.
            var newExpression = expression.Body as NewExpression ??
                                throw new ArgumentException("The expression must be an anonymous initialization, e.g. x => new { Name = x.Name, Age = x.Age + 5 }");

            var instance = new DmlExpressionRewriter(expression.Parameters);

            instance.AddSettersFromAnonymousConstructor(newExpression, "");
            return(PrepareExpression <TSource>(sourceExpression, instance._assignments));
        }
        public static Expression PrepareExpression <TSource, TTarget>(Expression sourceExpression, Expression <Func <TSource, TTarget> > expression)
        {
            if (expression == null)
            {
                throw new ArgumentNullException(nameof(expression));
            }

            var memberInitExpression = expression.Body as MemberInitExpression ??
                                       throw new ArgumentException("The expression must be a member initialization, e.g. x => new Dog { Name = x.Name, Age = x.Age + 5 }, " +
                                                                   // If someone call InsertSyntax<TSource>.As(source => new {...}), the code will fail here, so we have to hint at how to correctly
                                                                   // use anonymous initialization too.
                                                                   "or an anonymous initialization with an explicitly specified target type when inserting");

            if (memberInitExpression.Type != typeof(TTarget))
            {
                throw new TypeMismatchException($"Expecting an expression of exact type {typeof(TTarget).AssemblyQualifiedName} " +
                                                $"but got {memberInitExpression.Type.AssemblyQualifiedName}");
            }

            var instance = new DmlExpressionRewriter(expression.Parameters);

            instance.AddSettersFromBindings(memberInitExpression.Bindings, "");
            return(PrepareExpression <TSource>(sourceExpression, instance._assignments));
        }
示例#3
0
 /// <summary>
 /// Perform an <c>update versioned</c> on the entities. The update operation is performed in the database without reading the entities out of it.
 /// </summary>
 /// <returns>The number of updated entities.</returns>
 public int UpdateVersioned() => _source.ExecuteUpdate(DmlExpressionRewriter.PrepareExpression <TSource>(_source.Expression, _assignments.List), true);