具有超过16个参数的Lambda表达式Func

3

我需要编写一个在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);

只是好奇,因为我以前从未见过这种模式。为什么不能从较小的部分组成谓词呢? - adrianm
1个回答

2
我们的产品在 .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个通用类。


由于这将被转换为SQL调用,因此它无法工作。异常是:“无法将节点“值”格式化为SQL以执行”。 - Michael H.
@MichaelH。我明白了...我们从未遇到过这个问题,因为我们将结果lambda编译为内存中的评估。不过可能有另一种方法。你的BusinessDTO类的结构是否与你传递的参数列表相似?如果是的话,你可以使用它来代替一个泛型类的实例,这样或许会更好些。 - Sergey Kalinichenko

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接