3

I have to write a filtering lambda expression used in LinqToSQL, which requires more than the number of parameters provided by the standard System.Func (in this case the max number is 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) => ...

How can I achieve this?

Here is the usage:

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);
4

1 回答 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 个泛型类。

于 2011-12-12T20:29:05.113 回答