问题
Initially got following codes. columnA and columnB only need to update if got changes. (I have columnC and so on also but just elaborate short one here). But the updatedBy and updatedTime always need to update. The following way is doing checking for each changes and updating the columns correspondingly. But this is tedious.
if(isChangeColumnA && isChangeColumnB)
{
repo.partialUpdate(product, p=> p.columnA, p=> p.columnB, p=> p.UpdatedBy, p=> p.UpdatedTime)
}
else if (isChangeColumnA)
{
repo.partialUpdate(product, p=> p.columnA, p=> p.UpdatedBy, p=> p.UpdatedTime)
}else if(isChangeColumnB)
{
repo.partialUpdate(product, p=> p.columnB, p=> p.UpdatedBy, p=> p.UpdatedTime)
}
Would like to make it more dynamic by combining the the expression as params in the following way, so in the end, i just need to pass in the combined expressions as params with less hassle.
Expression<Func<Product, object>>[] updatedExpr = {p=> p.UpdatedBy, p=> p.UpdatedTime};
var combinedExpr = updatedExpr;
if (isChangeColumnA)
{
Expression<Func<Product, object>> columnAExpr = p => p.columnA;
combinedExpr = combinedExpr combine with columnAExpr
}else if(isChangeColumnB)
{
Expression<Func<Product, object>> columnBExpr = p => p.columnA;
combinedExpr = combinedExpr combine with columnBExpr
}
repo.partialUpdate(product,combinedExpr);
Additional info, the partial Update method is as follows:
public void PartialUpdate(TEntity entity, params Expression<Func<TEntity, object>>[] includeProperties)
{
...
}
来源:https://stackoverflow.com/questions/48701046/combine-multiple-expression-as-a-lambda-for-partial-update-expressionfunct-o