简体   繁体   English

如何传递表达式<Func<T, bool> &gt; 谓词作为方法的参数?

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

I have a higher order function for handling transaction:我有一个处理事务的高阶函数:

public static void ExecuteTransaction<Context, FunctionParameter>(Action<FunctionParameter> 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?:如何更改 ExecuteTransaction 方法以接受此方法及其参数?:

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

If I understood correctly, you are trying to pass DeleteBy to ExecuteTransaction , and let ExecuteTransaction run the method.如果我理解正确,您正在尝试将DeleteBy传递给ExecuteTransaction ,并让ExecuteTransaction运行该方法。

Assuming you have a Expression<Func<TEntity, bool>> to act as the parameter of DeleteBy prepared already:假设你有一个Expression<Func<TEntity, bool>>作为已经准备好的DeleteBy的参数:

Expression<Func<TEntity, bool>> param = entity => /*some lambda that returns a bool */;

you can pass DeleteBy directly, without any changes to ExecuteTransaction , because DeleteBy matches the signature of Action<T> :您可以直接传递DeleteBy ,而无需对ExecuteTransaction进行任何更改,因为DeleteBy匹配Action<T>的签名:

ExecuteTransaction(DeleteBy, param, context);

The compiler will automatically infer that FunctionParameter is Expression<Func<TEntity, bool>> .编译器会自动推断FunctionParameterExpression<Func<TEntity, bool>>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在表达式中添加额外的参数 <Func<T, bool> &gt;通用存储库中的谓词 - How to add extra parameter to Expression<Func<T, bool>> predicate in Generic Repository 如何将Expression <Func <T,bool >>转换为Predicate <T> - How to convert an Expression<Func<T, bool>> to a Predicate<T> 转换谓词 <T> 表达 <Func<T, bool> &gt; - Convert Predicate<T> to Expression<Func<T, bool>> 表达<Func<T, bool> &gt; 谓词两种类型 - Expression<Func<T, bool>> predicate two types 我可以动态创建一个表达式 <Func<T, bool> &gt;谓词,但我如何创建表达式 <Func<T1,T2,bool> &gt; - I can dynamically create an Expression<Func<T, bool>> predicate ,but how do i create Expression<Func<T1,T2,bool>> 如何转换Func <T, bool> 谓词 <T> ? - How to convert Func<T, bool> to Predicate<T>? 你能传递一个表达式吗 <Func<T, bool> &gt;谓词成linq在哪里声明? - Can you pass in an Expression<Func<T, bool>> predicate into a linq Where statement? 如何将谓词作为变量传递给Expression <Func<T> &gt; - How can pass predicate as variable to Expression<Func<T>> 表达 <Func<T,bool> &gt; vs Func <T,bool> 在方法重载 - Expression<Func<T,bool>> vs Func<T,bool> in method overloading 如何动态创建表达式<func<myclass, bool> &gt; 带有字符串参数的谓词? </func<myclass,> - How do I dynamically create an Expression<Func<MyClass, bool>> predicate with string parameter?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM