Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Linq 重写谓词生成器,使其更通用于创建动态where子句?_Linq_Asp.net Mvc 4_Generics_.net 4.0_Predicate - Fatal编程技术网

Linq 重写谓词生成器,使其更通用于创建动态where子句?

Linq 重写谓词生成器,使其更通用于创建动态where子句?,linq,asp.net-mvc-4,generics,.net-4.0,predicate,Linq,Asp.net Mvc 4,Generics,.net 4.0,Predicate,我想抓取对象(例如:Lead),并根据它们的顺序将其映射到搜索过滤器。而不是if/then语句并强制转换下面的搜索参数。所以lead_ID将映射到nvc[“search_0”],lead_编号将映射到nvc[“search_1”],等等 领导: 这是搜索筛选器和谓词设置: var predicate = PredicateBuilder.True<Fre_LeadSummaryDto>(); if (!String.IsNullOrEmpty(nv

我想抓取对象(例如:Lead),并根据它们的顺序将其映射到搜索过滤器。而不是if/then语句并强制转换下面的搜索参数。所以lead_ID将映射到nvc[“search_0”],lead_编号将映射到nvc[“search_1”],等等

领导:

这是搜索筛选器和谓词设置:

 var predicate = PredicateBuilder.True<Fre_LeadSummaryDto>();

                if (!String.IsNullOrEmpty(nvc["sSearch_0"]))
                {
                    leadId = nvc["sSearch_0"];
                    predicate = predicate.And(i => i.Lead_ID.Equals(leadId));
                }

                if (!String.IsNullOrEmpty(nvc["sSearch_1"]))
                {
                    leadNumber = nvc["sSearch_1"];
                    predicate = predicate.And(i =>   i.Lead_Number.StartsWith(leadNumber));
                }
谓词生成器:

/// <summary>   
    /// Enables the efficient, dynamic composition of query predicates.   
    /// </summary>   
    public static class PredicateBuilder
    {
        /// <summary>   
        /// Creates a predicate that evaluates to true.   
        /// </summary>   
        public static Expression<Func<T, bool>> True<T>() { return param => true; }

        /// <summary>   
        /// Creates a predicate that evaluates to false.   
        /// </summary>   
        public static Expression<Func<T, bool>> False<T>() { return param => false; }

        /// <summary>   
        /// Creates a predicate expression from the specified lambda expression.   
        /// </summary>   
        public static Expression<Func<T, bool>> Create<T>(Expression<Func<T, bool>> predicate) { return predicate; }

        /// <summary>   
        /// Combines the first predicate with the second using the logical "and".   
        /// </summary>   
        public static Expression<Func<T, bool>> And<T>(this Expression<Func<T, bool>> first, Expression<Func<T, bool>> second)
        {
            return first.Compose(second, Expression.AndAlso);
        }

        /// <summary>   
        /// Combines the first predicate with the second using the logical "or".   
        /// </summary>   
        public static Expression<Func<T, bool>> Or<T>(this Expression<Func<T, bool>> first, Expression<Func<T, bool>> second)
        {
            return first.Compose(second, Expression.OrElse);
        }

        /// <summary>   
        /// Negates the predicate.   
        /// </summary>   
        public static Expression<Func<T, bool>> Not<T>(this Expression<Func<T, bool>> expression)
        {
            var negated = Expression.Not(expression.Body);
            return Expression.Lambda<Func<T, bool>>(negated, expression.Parameters);
        }

        /// <summary>   
        /// Combines the first expression with the second using the specified merge function.   
        /// </summary>   
        static Expression<T> Compose<T>(this Expression<T> first, Expression<T> second, Func<Expression, Expression, Expression> merge)
        {
            // zip parameters (map from parameters of second to parameters of first)   
            var map = first.Parameters
                .Select((f, i) => new { f, s = second.Parameters[i] })
                .ToDictionary(p => p.s, p => p.f);

            // replace parameters in the second lambda expression with the parameters in the first   
            var secondBody = ParameterRebinder.ReplaceParameters(map, second.Body);

            // create a merged lambda expression with parameters from the first expression   
            return Expression.Lambda<T>(merge(first.Body, secondBody), first.Parameters);
        }

        class ParameterRebinder : ExpressionVisitor
        {
            readonly Dictionary<ParameterExpression, ParameterExpression> map;

            ParameterRebinder(Dictionary<ParameterExpression, ParameterExpression> map)
            {
                this.map = map ?? new Dictionary<ParameterExpression, ParameterExpression>();
            }

            public static Expression ReplaceParameters(Dictionary<ParameterExpression, ParameterExpression> map, Expression exp)
            {
                return new ParameterRebinder(map).Visit(exp);
            }

            protected override Expression VisitParameter(ParameterExpression p)
            {
                ParameterExpression replacement;

                if (map.TryGetValue(p, out replacement))
                {
                    p = replacement;
                }

                return base.VisitParameter(p);
            }
        }
    }  
//
///支持查询谓词的高效、动态组合。
///    
公共静态类谓词生成器
{
///    
///创建一个计算结果为true的谓词。
///    
公共静态表达式True(){return param=>True;}
///    
///创建计算结果为false的谓词。
///    
公共静态表达式False(){return param=>False;}
///    
///从指定的lambda表达式创建谓词表达式。
///    
公共静态表达式创建(表达式谓词){返回谓词;}
///    
///使用逻辑“and”组合第一个谓词和第二个谓词。
///    
公共静态表达式和(第一个表达式,第二个表达式)
{
首先返回.Compose(第二个是Expression.AndAlso);
}
///    
///使用逻辑“or”组合第一个谓词和第二个谓词。
///    
公共静态表达式或(第一个表达式,第二个表达式)
{
首先返回.Compose(第二个是Expression.OrElse);
}
///    
///否定谓词。
///    
公共静态表达式不是(此表达式)
{
var negated=Expression.Not(Expression.Body);
返回表达式.Lambda(否定,表达式.Parameters);
}
///    
///使用指定的合并函数将第一个表达式与第二个表达式合并。
///    
静态表达式组合(先此表达式,后表达式,Func merge)
{
//zip参数(从第二个参数映射到第一个参数)
var map=first.Parameters
.Select((f,i)=>new{f,s=second.Parameters[i]})
.ToDictionary(p=>p.s,p=>p.f);
//用第一个lambda表达式中的参数替换第二个lambda表达式中的参数
var secondBody=ParameterRebinder.ReplaceParameters(映射,second.Body);
//使用第一个表达式中的参数创建合并的lambda表达式
返回表达式.Lambda(merge(first.Body,secondBody),first.Parameters);
}
类参数reBinder:ExpressionVisitor
{
只读字典地图;
ParameterRebinder(字典映射)
{
this.map=map??新建字典();
}
公共静态表达式替换参数(字典映射、表达式表达式)
{
返回新参数浏览器(map)。访问(exp);
}
受保护的重写表达式VisitParameter(ParameterExpression p)
{
参数表达替换;
if(映射TryGetValue(p,输出替换))
{
p=替换;
}
返回基访问参数(p);
}
}
}  
 leads = leads.Where(predicate);
/// <summary>   
    /// Enables the efficient, dynamic composition of query predicates.   
    /// </summary>   
    public static class PredicateBuilder
    {
        /// <summary>   
        /// Creates a predicate that evaluates to true.   
        /// </summary>   
        public static Expression<Func<T, bool>> True<T>() { return param => true; }

        /// <summary>   
        /// Creates a predicate that evaluates to false.   
        /// </summary>   
        public static Expression<Func<T, bool>> False<T>() { return param => false; }

        /// <summary>   
        /// Creates a predicate expression from the specified lambda expression.   
        /// </summary>   
        public static Expression<Func<T, bool>> Create<T>(Expression<Func<T, bool>> predicate) { return predicate; }

        /// <summary>   
        /// Combines the first predicate with the second using the logical "and".   
        /// </summary>   
        public static Expression<Func<T, bool>> And<T>(this Expression<Func<T, bool>> first, Expression<Func<T, bool>> second)
        {
            return first.Compose(second, Expression.AndAlso);
        }

        /// <summary>   
        /// Combines the first predicate with the second using the logical "or".   
        /// </summary>   
        public static Expression<Func<T, bool>> Or<T>(this Expression<Func<T, bool>> first, Expression<Func<T, bool>> second)
        {
            return first.Compose(second, Expression.OrElse);
        }

        /// <summary>   
        /// Negates the predicate.   
        /// </summary>   
        public static Expression<Func<T, bool>> Not<T>(this Expression<Func<T, bool>> expression)
        {
            var negated = Expression.Not(expression.Body);
            return Expression.Lambda<Func<T, bool>>(negated, expression.Parameters);
        }

        /// <summary>   
        /// Combines the first expression with the second using the specified merge function.   
        /// </summary>   
        static Expression<T> Compose<T>(this Expression<T> first, Expression<T> second, Func<Expression, Expression, Expression> merge)
        {
            // zip parameters (map from parameters of second to parameters of first)   
            var map = first.Parameters
                .Select((f, i) => new { f, s = second.Parameters[i] })
                .ToDictionary(p => p.s, p => p.f);

            // replace parameters in the second lambda expression with the parameters in the first   
            var secondBody = ParameterRebinder.ReplaceParameters(map, second.Body);

            // create a merged lambda expression with parameters from the first expression   
            return Expression.Lambda<T>(merge(first.Body, secondBody), first.Parameters);
        }

        class ParameterRebinder : ExpressionVisitor
        {
            readonly Dictionary<ParameterExpression, ParameterExpression> map;

            ParameterRebinder(Dictionary<ParameterExpression, ParameterExpression> map)
            {
                this.map = map ?? new Dictionary<ParameterExpression, ParameterExpression>();
            }

            public static Expression ReplaceParameters(Dictionary<ParameterExpression, ParameterExpression> map, Expression exp)
            {
                return new ParameterRebinder(map).Visit(exp);
            }

            protected override Expression VisitParameter(ParameterExpression p)
            {
                ParameterExpression replacement;

                if (map.TryGetValue(p, out replacement))
                {
                    p = replacement;
                }

                return base.VisitParameter(p);
            }
        }
    }