[英]Remove items from a collection if one date is greater than another
我正在嘗試使用linq從一個日期大於另一個日期的集合中刪除項目。 我知道我需要使用DateTime.Compare()
但我不確定如何處理。
如果允許這樣做,我的代碼將如下所示:
ciLibrary.RemoveAll(x => x.LastSyncToWebsite >= x.Modified);
由於這不適用於DATETIME
值,因此如何正確表示此語句?
可以使用>=
比較DateTime
值。 你需要的是一個Where
:
clLibrary.Where(x => !(x.LastSyncToWebsite >= x.Modified))
或等效地:
clLibrary.Where(x => x.LastSyncToWebsite < x.Modified)
這將生成一個新的IEnumerable,而clLibrary
保持不變。 如果clLibrary
的類型為List<T>
,則可以:
clLibrary = clLibrary.Where(x => x.LastSyncToWebsite < x.Modified).ToList();
如果clLibrary
的類型為T[]
,則可以:
clLibrary = clLibrary.Where(x => x.LastSyncToWebsite < x.Modified).ToArray();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.