【问题标题】:Dynamic Where condition with QueryoverQueryover 的动态 Where 条件
【发布时间】:2012-06-05 12:08:39
【问题描述】:

我有一个应用程序,我正在尝试实现 DDD 概念。我的存储库类有一些列出实体的方法。我想知道如何使用 QueryOver 进行查询以使用AND 运算符进行过滤分离,当参数填充时,示例

public IEnumerable<Product> FindProducts(string name, decimal? price, DateTime? validDate, int? stock, int? idSupplier)
{
   var query = Session.QueryOver<Product>().OrderBy(x => x.Name).Asc;

   if (!string.IsNullOrEmpty(name))
      // add where condition for name parameter

   if (price.HasValue)
      // add 'AND' where condition for price parameter

   if (validDate.HasValue)
      // add 'AND' where condition for validDate parameter

   if (idSupplier.HasValue)
      // add 'AND' where condition for idSupplier parameter

   // other possible conditions

   return query.List();
}

在我使用 HQL 字符串查询之前有什么办法吗?呵呵

谢谢!

【问题讨论】:

    标签: c# .net nhibernate orm queryover


    【解决方案1】:

    在这里,使用 PredicateBuilder:

    如何:

    IQueryable<Product> SearchProducts (params string[] keywords)
    {
      var predicate = PredicateBuilder.False<Product>();
    
      foreach (string keyword in keywords)
      {
        string temp = keyword;
        predicate = predicate.Or (p => p.Description.Contains (temp));
      }
      return dataContext.Products.Where (predicate);
    }
    

    PredicateBuilder 来源:

    using System;
    using System.Linq;
    using System.Linq.Expressions;
    using System.Collections.Generic;
    
    public static class PredicateBuilder
    {
      public static Expression<Func<T, bool>> True<T> ()  { return f => true;  }
      public static Expression<Func<T, bool>> False<T> () { return f => false; }
    
      public static Expression<Func<T, bool>> Or<T> (this Expression<Func<T, bool>> expr1,
                                                          Expression<Func<T, bool>> expr2)
      {
        var invokedExpr = Expression.Invoke (expr2, expr1.Parameters.Cast<Expression> ());
        return Expression.Lambda<Func<T, bool>>
              (Expression.OrElse (expr1.Body, invokedExpr), expr1.Parameters);
      }
    
      public static Expression<Func<T, bool>> And<T> (this Expression<Func<T, bool>> expr1,
                                                           Expression<Func<T, bool>> expr2)
      {
        var invokedExpr = Expression.Invoke (expr2, expr1.Parameters.Cast<Expression> ());
        return Expression.Lambda<Func<T, bool>>
              (Expression.AndAlso (expr1.Body, invokedExpr), expr1.Parameters);
      }
    }
    

    有关 PredicateBuilder 和 LinqKit 的更多信息,请访问:http://www.albahari.com/nutshell/linqkit.aspx

    【讨论】:

    • 是的,我知道我可以做第二种方式,但是我怎样才能在其中添加其他可能的条件?
    • @felipeoriani 阅读了谓词构建器,假设您可以使用 3rd 方库(很多人使用它,它很安全;-),那么我会使用它。
    • 很棒的代码,我会试试的,谢谢:)...我不知道这个库,我在哪里可以找到它?谢谢[]s
    • 只需使用我放在这里的代码,在您自己的解决方案中使用它。 lib 本身就是这段代码。您可以引用它,或者将其包含在您自己的项目中,以便您可以更轻松地操作它!另外,不要忘记投票/标记为答案;)有关 PredicateBuilder 的更多信息可在此处获得:albahari.com/nutshell/predicatebuilder.aspx
    • 有关 PredicateBuilder 的更多信息 - albahari.com/nutshell/predicatebuilder.aspx 更多使用示例等等
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-10-13
    • 2011-10-20
    • 1970-01-01
    • 1970-01-01
    • 2019-03-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多