[英]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.now
或datetime.minvalue
但我不明白为什么在tile.Item_Business_Unit
之后存在条件表达式,因为两个条件执行相同的操作。 即使它有一个空业务单位,项目也会被选中吗? 这是否与正常的if / else操作不同?
任何建议将非常感激。
您是否被快捷方式符号抛出?
x = (test_case) ? (true_part) : (false_part);
如果test_case
计算结果为true ,那么就可以了
而如果test_case
计算结果为false , test_case
评估此表达式
更新:
作为一个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.