繁体   English   中英

不了解LINQ查询

[英]Not understanding LINQ query

我正在维护一个项目并遇到了一些我无法理解的代码。 LINQ查询:

var toDraw = from tile in testArr.AsEnumerable()
             where tile.Item_Business_Unit != null ? 
             ((tile.Ending_Date >= DateTime.Now || tile.Ending_Date == DateTime.MinValue) && 
             ((tile.Sales_Code == null) || (tile.Sales_Code.ToString() == customerNumber) || 
             (tile.Sales_Code.ToString() == cpg)) && (tile.Unit_Price != 0)) : 
             ((tile.Ending_Date >= DateTime.Now || tile.Ending_Date == DateTime.MinValue) && 
             ((tile.Sales_Code == null) || (tile.Sales_Code.ToString() == customerNumber) || 
             (tile.Sales_Code.ToString() == cpg)) && (tile.Unit_Price != 0))
                             select tile;

根据我的理解,从数组中选择一个具有以下标准的图块:

  • 结束日期可以是datetime.nowdatetime.minvalue
  • 销售代码可以为null,也可以等于customer no或cpg
  • 单价应大于0

但我不明白为什么在tile.Item_Business_Unit之后存在条件表达式,因为两个条件执行相同的操作。 即使它有一个空业务单位,项目也会被选中吗? 这是否与正常的if / else操作不同?

任何建议将非常感激。

您是否被快捷方式符号抛出?

x = (test_case) ? (true_part) : (false_part);

如果test_case计算结果为true ,那么就可以了

true_part

而如果test_case计算结果为falsetest_case评估此表达式

false_part

更新:

作为一个FYI:上面条件表达式双方的结果测试是相同的,因此甚至不需要神秘的代码。

你可以用这个代替:

var toDraw = from tile in testArr.AsEnumerable()
    where 
    ((tile.Ending_Date >= DateTime.Now || tile.Ending_Date == DateTime.MinValue) &&
    ((tile.Sales_Code == null) || (tile.Sales_Code.ToString() == customerNumber) || (tile.Sales_Code.ToString() == cpg)) &&
    (tile.Unit_Price != 0)) 
    select tile;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM