[英]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.