【问题标题】:LINQ: IF condition in WHERE clause not workingLINQ:WHERE 子句中的 IF 条件不起作用
【发布时间】:2016-11-21 07:46:00
【问题描述】:

我有以下 LINQ 查询,它应该查看表 Products 并向我返回传递参数的记录,这些记录是:搜索词(字符串)和不同“类型”的 3 个布尔值。

   var query = context.Products
                       .Where(a => request.SearchTerm == null || a.Name.Contains(request.SearchTerm))
    .Where(a => (request.isTypeA == false || (a.OrderType == "X" && request.isTypeA == true)) ||
    (request.typeB == false || (a.OrderType == "R" && request.typeB == true))
    || (request.typeC == false || (a.OrderType == "D" && request.typeC == true)))
    .Where (a=> a.OrderType != "U")
    .Where(a => a.IsInactiveFlag == false )
    .OrderBy(a => a.OrderType)
    .Select(c => new ProductType   
        {
            ProductTypeId = c.ProductTypeId,
            IsSelected = false,
            OrderType = c.OrderType,
            Name = c.Name,
            IsInactiveFlag = c.IsInactiveFlag
        });

问题:问题是查询总是通过查看传递的 searchTerm 返回记录,但它没有考虑布尔参数。所以比如说我的搜索参数是:searchTerm = "reference " , isTypeA = false, isTypeB = false 和 isTypeC = true。上面的查询将返回所有不同类型的 searchTerm "reference" 的所有记录,而不仅仅是 TypeC。

在发布这个问题之前我搜索了很多,但没有什么是我遇到的。请让我知道我做错了什么。 谢谢!

【问题讨论】:

    标签: c# mysql sql linq


    【解决方案1】:

    您应该在过滤器值的每个测试旁边明确包含/排除关于 OrderType 的二元标准,它用作开关......

      var query = context.Products
                           .Where(a => request.SearchTerm == null || a.Name.Contains(request.SearchTerm))
        .Where(a => (request.isTypeA == false && a.OrderType != "X" ) || (a.OrderType == "X" && request.isTypeA == true)) ||
        (request.typeB == false && a.OrderType != "R" )|| (a.OrderType == "R" && request.typeB == true))
        || (request.typeC == false && a.OrderType != "D" ) || (a.OrderType == "D" && request.typeC == true)))
        .Where (a=> a.OrderType != "U")
        .Where(a => a.IsInactiveFlag == false )
        .OrderBy(a => a.OrderType)
        .Select(c => new ProductType   
            {
                ProductTypeId = c.ProductTypeId,
                IsSelected = false,
                OrderType = c.OrderType,
                Name = c.Name,
                IsInactiveFlag = c.IsInactiveFlag
            });
    

    【讨论】:

      【解决方案2】:

      试试这个

         var query = context.Products
                         .Where(a => request.SearchTerm == null || a.Name.Contains(request.SearchTerm))
      .Where(a => (a.OrderType == "X" && request.isTypeA)
      || (a.OrderType == "R" && request.typeB)
      || (a.OrderType == "D" && request.typeC))
      .Where (a=> a.OrderType != "U")
      .Where(a => a.IsInactiveFlag == false )
      .OrderBy(a => a.OrderType)
      .Select(c => new ProductType   
          {
              ProductTypeId = c.ProductTypeId,
              IsSelected = false,
              OrderType = c.OrderType,
              Name = c.Name,
              IsInactiveFlag = c.IsInactiveFlag
          });
      

      【讨论】:

      • 您正在对所有条件执行 OR ( || ),因此您的括号是无用的,如果仅满足一个条件 ( request.isTypeX == false ),这将给出一个 true 并验证条件。
      • 好的,我试过你的建议,它仍然返回不同“类型”的所有记录
      • 那么你有什么建议,我应该如何重写这个查询,使其不返回所有记录? @yeska
      • 效果很好!我不太确定我原来的查询出了什么问题。
      • 您想按 IsTypeC = true 进行过滤。想象一下它是假的,你的条件将返回真,因为你在 IdTypeC=false 上做一个 or 运算符,当括号内外有相同类型的运算符时,括号是无用的
      猜你喜欢
      • 2017-07-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-02
      • 2023-03-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多