Хорошо, вот что я в итоге сделал.Я нашел эту функцию , которая, кажется, делает свое дело.
public static void SetEntityValue(TDBTable entity, Expression<Func<TDBTable, object>> expression, object value)
{
ParameterExpression valueParameterExpression = Expression.Parameter(typeof(object));
Expression targetExpression = expression.Body is UnaryExpression ? ((UnaryExpression)expression.Body).Operand : expression.Body;
var newValue = Expression.Parameter(expression.Body.Type);
var assign = Expression.Lambda<Action<TDBTable, object>>
(
Expression.Assign(targetExpression, Expression.Convert(valueParameterExpression, targetExpression.Type)),
expression.Parameters.Single(),
valueParameterExpression
);
assign.Compile().Invoke(entity, value);
}
Я вызываю ее в своей функции обновления
public T Update(TDBTable entity, Expression<Func<TDBTable, object>> expression, object value,
Expression<Func<TDBTable, bool>> predicate)
{
var dbEntity = await GetOneAsync(predicate); // Which fetches me the entity to change
// Sets the variable
SetEntityValue(result, expression, value);
// Update Entity
result = await EditAsync(result);
return entity;
}
Я называю это так
Обновление (новый Customer (), x => x.FirstName, "John", x => x.Id == 4);