【问题标题】:How can I compose a LINQ query with the help of a predicate builder如何在谓词构建器的帮助下编写 LINQ 查询
【发布时间】:2016-03-10 10:32:38
【问题描述】:

我真的很想分享这段代码,因为我认为它很有用。

所以,假设您想以编程方式创建一个 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();

【问题讨论】:

  • 实际上已经有一个方便的库,称为Linqkit,但我想使用你的代码更轻量级。
  • 感谢您的意见。我不知道。

标签: c# linq predicate query-builder


【解决方案1】:

代码可以在如下帮助类的帮助下实现:

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;
        }
    }
}

【讨论】:

  • 但这只是建立And 标准,相当于链接多个.Where
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2010-09-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多