簡體   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