Combine multiple expression as a lambda for partial Update: Expression<Func<T, object>>

浪子不回头ぞ 提交于 2020-01-06 12:28:06

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!