linq-to-sql - 具有超过 16 个参数的 Lambda 表达式 Func

标签 linq-to-sql lambda linq-expressions

我必须编写一个在 LinqToSQL 中使用的过滤 lambda 表达式,它需要的参数数量超过标准 System.Func 提供的参数数量(在本例中最大数量为 16)。

Expression<Func<BusinessDTO,
                                string,
                                int,
                                int,
                                int,
                                DateTime,
                                DateTime,
                                DateTime,
                                DateTime,
                                DateTime,
                                DateTime,
                                DateTime,
                                DateTime,
                                int,
                                int,
                                int,
                                int,//-> Max number exceeded 
                                bool>> fnFilter = (business,
                                                        Name,
                                                        Redeemed,
                                                        Permanent,
                                                        ApprovedByUser,
                                                        BeginApprovalDate,
                                                        EndApprovalDate,
                                                        BeginExpiryDate,
                                                        EndExpiryDate,
                                                        BeginEntryDate,
                                                        EndEntryDate,
                                                        BeginChangeDate,
                                                        EndChangeDate,
                                                        WorkFlowCode,
                                                        CreatedByUser,
                                                        UpdatedByUser) => ...

我怎样才能做到这一点?

用法如下:

filterExpression = Expression.Invoke(fnFilter, businessParam,
                                   Expression.Constant(name),
                                   Expression.Constant(redeemed),
                                   Expression.Constant(permanent),
                                   Expression.Constant(approvedByUser),
                                   Expression.Constant(filter.BeginApprovalDate),
                                   Expression.Constant(filter.EndApprovalDate),
                                   Expression.Constant(filter.BeginExpiryDate),
                                   Expression.Constant(filter.EndExpiryDate),
                                   Expression.Constant(filter.BeginEntryDate),
                                   Expression.Constant(filter.EndEntryDate),
                                   Expression.Constant(filter.BeginChangeDate),
                                   Expression.Constant(filter.EndChangeDate),
                                   Expression.Constant(workflowCode),
                                   Expression.Constant(createdByUser),
                                   Expression.Constant(updatedByUser));

var resultExpression = Expression.Equal(filterExpression, Expression.Constant(true));
            var predicate = Expression.Lambda<Func<BusinessDTO, bool>>(resultExpression, businessParam);

repository.FindAll(predicate);

最佳答案

我们的产品在 .NET 3.5 时代就遇到了这个限制,当时限制是 4 个。我们最终将额外的参数分组到一个对象中,为此我们创建了一个具有 32 个类型参数的巨大泛型类型:

class FilterParams<T1,T2,T3,T4,/*..*/,T32> {
    public T1 t1Val {get;set;}
    public T2 t1Val {get;set;}
    public T3 t1Val {get;set;}
    // ...
}

Expression<Func<BusinessDTO,FilterParams<
                    string,
                    int,
                    int,
                    int,
                    DateTime,
                    DateTime,
                    DateTime,
                    DateTime,
                    DateTime,
                    DateTime,
                    DateTime,
                    DateTime,
                    int,
                    int,
                    int,
                    int,//-> Max number is not exceeded :)
                    bool,object,object,/* pad to 32 ...*/>> fnFilter = (business,
                         filterParams) => business.Name.Equals(filterParams.t0val) && ...

var fp = new FilterParams</*ugly type parameter list*/>(
    t0val = name,
    t1val = redeemed,
    t2val = permanent,
    // ...and so on
);

filterExpression = Expression.Invoke(fnFilter, businessParam, Expression.Constant(fp));

// ...the rest is the same as in your post

这对我们来说效果很好。最终我们将它扩展到 64 个,并创建了 64 个不同的类以避免用 object 填充类型参数,如上所示。当然,我们编写了一个小脚本来生成这 64 个泛型类。

关于linq-to-sql - 具有超过 16 个参数的 Lambda 表达式 Func,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8480200/

相关文章:

linq-to-sql - Linq to Sql 连接两个表

c# - 使用 lambdas 选择子对象集合

java - 在android studio中的java 8中使用lambda

Haskell:where子句引用lambda中的绑定(bind)变量

c# - 如何为 IEnumerable.Any 编写 Linq.Expression

c# - 如何在 C# 表达式中设置参数

c# - 不能使用三元运算符给 Linq 表达式赋值

c# - Linq-To-SQL 中的 Hacker News 风格排序算法

c# - LINQ - .Cast<T>() 选择记录吗?

c# - 持久性无知 Linq to SQL