如何将 Expression<Func<T, bool>> 谓词作为参数传递给方法?

huangapple go评论67阅读模式
英文:

How to pass Expression<Func<T, bool>> predicate as a parameter to a method?

问题

我可以帮你翻译以下代码部分:

我有一个用于处理事务的高阶函数:

public static void ExecuteTransaction&lt;Context, FunctionParameter&gt;(Action&lt;FunctionParameter&gt; functionToBeExecuted,
                                                                           FunctionParameter parameter,
                                                                           Context context)
                                                                           where Context : DbContext
        {
            using (var transaction = context.Database.BeginTransaction())
            {
                try
                {
                    functionToBeExecuted(parameter);
                    context.SaveChanges();

                    transaction.Commit();
                }
                catch (Exception)
                {
                    transaction.Rollback();
                    throw;
                }
            }
        }

如何更改 ExecuteTransaction 方法以接受此方法及其参数?:

public void DeleteBy(Expression&lt;Func&lt;TEntity, bool&gt;&gt; predicate)
{
    var results = DbSet.Where(predicate).ToList();
    DbSet.RemoveRange(results);
}
英文:

I have a higher order function for handling transaction:

public static void ExecuteTransaction&lt;Context, FunctionParameter&gt;(Action&lt;FunctionParameter&gt; functionToBeExecuted,
                                                                           FunctionParameter parameter,
                                                                           Context context)
                                                                           where Context : DbContext
        {
            using (var transaction = context.Database.BeginTransaction())
            {
                try
                {
                    functionToBeExecuted(parameter);
                    context.SaveChanges();

                    transaction.Commit();
                }
                catch (Exception)
                {
                    transaction.Rollback();
                    throw;
                }
            }
        }

How can i change the ExecuteTransaction method to accept this method and its parameter?:

public void DeleteBy(Expression&lt;Func&lt;TEntity, bool&gt;&gt; predicate)
{
    var results = DbSet.Where(predicate).ToList();
    DbSet.RemoveRange(results);
}

答案1

得分: 2

如果我理解正确,您试图将DeleteBy传递给ExecuteTransaction,并让ExecuteTransaction运行该方法。

假设您已经准备好一个Expression&lt;Func&lt;TEntity, bool&gt;&gt;来作为DeleteBy的参数:

Expression&lt;Func&lt;TEntity, bool&gt;&gt; param = entity =&gt; /*一些返回bool的lambda表达式*/;

您可以直接传递DeleteBy,无需对ExecuteTransaction进行任何更改,因为DeleteByAction&lt;T&gt;的签名匹配:

ExecuteTransaction(DeleteBy, param, context);

编译器将自动推断FunctionParameterExpression&lt;Func&lt;TEntity, bool&gt;&gt;

英文:

If I understood correctly, you are trying to pass DeleteBy to ExecuteTransaction, and let ExecuteTransaction run the method.

Assuming you have a Expression&lt;Func&lt;TEntity, bool&gt;&gt; to act as the parameter of DeleteBy prepared already:

Expression&lt;Func&lt;TEntity, bool&gt;&gt; param = entity =&gt; /*some lambda that returns a bool */;

you can pass DeleteBy directly, without any changes to ExecuteTransaction, because DeleteBy matches the signature of Action&lt;T&gt;:

ExecuteTransaction(DeleteBy, param, context);

The compiler will automatically infer that FunctionParameter is Expression&lt;Func&lt;TEntity, bool&gt;&gt;.

huangapple
  • 本文由 发表于 2020年1月6日 20:09:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/59611867.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定