小编Rod*_*Rod的帖子

如何组合多个 c# Lambda 表达式 (Expression<Func<T, T>>)

我有以下函数,它实际上是 Z.EntityFramework.Plus 批量更新的包装器:

    public static int UpdateBulk<T>(this IQueryable<T> query, Expression<Func<T, T>> updateFactory) where T : IBaseEntity, new()
    {
        Expression<Func<T, T>> modifiedExpression = x => new T() { ModifiedBy = "Test", ModifiedDate = DateTime.Now };
        var combine = Expression.Lambda<Func<T, T>>(
            Expression.AndAlso(
                Expression.Invoke(updateFactory, updateFactory.Parameters),
                Expression.Invoke(modifiedExpression, modifiedExpression.Parameters)
            ),
            updateFactory.Parameters.Concat(modifiedExpression.Parameters)
        );  //This returns an error

        return query.Update(combine);
    }
Run Code Online (Sandbox Code Playgroud)

像这样调用:

        decimal probId = ProbId.ParseDecimal();

        db.Problems
            .Where(e => e.ProbId == probId)
            .UpdateBulk(e => new Problem() {
                CatId = Category.ParseNullableInt(),
                SubCatId = SubCategory.ParseNullableInt(),
                ListId = Problem.ParseNullableInt()
            }); …
Run Code Online (Sandbox Code Playgroud)

c# linq expression entity-framework-plus

5
推荐指数
1
解决办法
4046
查看次数

标签 统计

c# ×1

entity-framework-plus ×1

expression ×1

linq ×1