C# 如何在谓词生成器的帮助下编写LINQ查询

C# 如何在谓词生成器的帮助下编写LINQ查询,c#,linq,predicate,query-builder,C#,Linq,Predicate,Query Builder,实际上,我想分享这段代码,因为我认为它非常有用 因此,假设您希望通过编程创建一个linq查询 这将是非常好的,有一个可读的格式,不是吗 我们可以创建如下代码: PredicateBuilder<Document> filter = new PredicateBuilder<Document>(); filter.Add(x => x.IsDeleted == false); filter.Add(x => x.IsArchive == false); if

实际上,我想分享这段代码,因为我认为它非常有用

因此,假设您希望通过编程创建一个linq查询

这将是非常好的,有一个可读的格式,不是吗

我们可以创建如下代码:

PredicateBuilder<Document> filter = new PredicateBuilder<Document>();
filter.Add(x => x.IsDeleted == false);
filter.Add(x => x.IsArchive == false);

if (ucSearch.SearchText != "")
    filter.Add(x => x.DocumentName.Contains(ucSearch.SearchText));
repository.GetAll().Where(filter.GetLambda()).ToList();
PredicateBuilder过滤器=新的PredicateBuilder();
filter.Add(x=>x.IsDeleted==false);
filter.Add(x=>x.IsArchive==false);
如果(ucSearch.SearchText!=“”)
filter.Add(x=>x.DocumentName.Contains(ucSearch.SearchText));
repository.GetAll().Where(filter.GetLambda()).ToList();

该代码可以在助手类的帮助下实现,如下所示:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;

namespace Extensions
{
    public class PredicateBuilder<T> : List<Expression<Func<T, bool>>>
    {
        private class ParameterReplacer : ExpressionVisitor
        {
            private readonly ParameterExpression _parameter;
            public ParameterReplacer(ParameterExpression parameter)
            {
                _parameter = parameter;
            }
            protected override Expression VisitParameter(ParameterExpression node)
            {
                return base.VisitParameter(_parameter);
            }
        }
        public Expression<Func<T, bool>> GetLambda()
        {
            if (this.Count > 0)
            {
                var type = Expression.Parameter(typeof(T));
                if (this.Count > 1)
                {
                    BinaryExpression binaryExpression = Expression.MakeBinary(ExpressionType.And,
                        new ParameterReplacer(type).Visit(this.First().Body),
                        new ParameterReplacer(type).Visit(this.Skip(1).First().Body));
                    if (this.Count > 2)
                    {
                        foreach (var ex in this.ToList().Skip(2))
                            binaryExpression = Expression.And(binaryExpression, new ParameterReplacer(type).Visit(ex.Body));
                    }
                    return Expression.Lambda<Func<T, bool>>(binaryExpression, type);
                }
                else
                    return Expression.Lambda<Func<T, bool>>(new ParameterReplacer(type).Visit(this.First().Body), type);
            }
            return null;
        }
        public Func<T, bool> GetPredicate()
        {

            if (this.Count > 0)
            {
                var type = Expression.Parameter(typeof(T));
                if (this.Count > 1)
                {
                    BinaryExpression binaryExpression = Expression.MakeBinary(ExpressionType.And,
                        new ParameterReplacer(type).Visit(this.First().Body),
                        new ParameterReplacer(type).Visit(this.Skip(1).First().Body));
                    if (this.Count > 2)
                    {
                        foreach (var ex in this.ToList().Skip(2))
                            binaryExpression = Expression.And(binaryExpression, new ParameterReplacer(type).Visit(ex.Body));
                    }
                    return Expression.Lambda<Func<T, bool>>(binaryExpression, type).Compile();
                }
                else
                    return Expression.Lambda<Func<T, bool>>(new ParameterReplacer(type).Visit(this.First().Body), type).Compile();
            }
            return null;
        }
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用System.Linq.Expressions;
使用系统文本;
命名空间扩展
{
公共类谓词生成器:列表
{
私有类参数替换器:ExpressionVisitor
{
私有只读参数expression\u参数;
公共参数替换器(ParameterExpression参数)
{
_参数=参数;
}
受保护的重写表达式VisitParameter(ParameterExpression节点)
{
返回base.VisitParameter(_参数);
}
}
公共表达式GetLambda()
{
如果(this.Count>0)
{
var type=表达式参数(typeof(T));
如果(this.Count>1)
{
BinaryExpression BinaryExpression=Expression.MakeBinary(ExpressionType.And,
新参数replacer(type).Visit(this.First().Body),
新参数replacer(type).Visit(this.Skip(1.First().Body));
如果(this.Count>2)
{
foreach(这个.ToList()中的var ex.Skip(2))
binaryExpression=Expression.And(binaryExpression,新参数replacer(type).Visit(ex.Body));
}
返回表达式.Lambda(二进制表达式,类型);
}
其他的
返回表达式.Lambda(新参数replacer(type).Visit(this.First().Body),type);
}
返回null;
}
public Func GetPredicate()
{
如果(this.Count>0)
{
var type=表达式参数(typeof(T));
如果(this.Count>1)
{
BinaryExpression BinaryExpression=Expression.MakeBinary(ExpressionType.And,
新参数replacer(type).Visit(this.First().Body),
新参数replacer(type).Visit(this.Skip(1.First().Body));
如果(this.Count>2)
{
foreach(这个.ToList()中的var ex.Skip(2))
binaryExpression=Expression.And(binaryExpression,新参数replacer(type).Visit(ex.Body));
}
返回表达式.Lambda(二进制表达式,类型).Compile();
}
其他的
返回表达式.Lambda(新参数replacer(type).Visit(this.First().Body),type.Compile();
}
返回null;
}
}
}

实际上已经有了一个方便的库来调用它,但我认为使用您的代码更轻量级。谢谢您的输入。我不知道,但这只是建立了
标准,这相当于链接多个
。其中