简体   繁体   English

如果一个日期大于另一个日期,则从集合中删除项目

[英]Remove items from a collection if one date is greater than another

I'm trying to use linq to remove items from a collection where one date is greater than another. 我正在尝试使用linq从一个日期大于另一个日期的集合中删除项目。 I know I need to use DateTime.Compare() but I'm a bit unsure how to go about it. 我知道我需要使用DateTime.Compare()但我不确定如何处理。

If this were allowed, my code would look like the following: 如果允许这样做,我的代码将如下所示:

ciLibrary.RemoveAll(x => x.LastSyncToWebsite >= x.Modified);

Since this doesn't work with DATETIME values how do I properly represent this statement? 由于这不适用于DATETIME值,因此如何正确表示此语句?

DateTime values can be compared using >= . 可以使用>=比较DateTime值。 What you need is a Where : 你需要的是一个Where

clLibrary.Where(x => !(x.LastSyncToWebsite >= x.Modified))

or equivalently: 或等效地:

clLibrary.Where(x => x.LastSyncToWebsite < x.Modified)

This generates a new IEnumerable, leaving clLibrary unchanged. 这将生成一个新的IEnumerable,而clLibrary保持不变。 If clLibrary is of type List<T> , you can: 如果clLibrary的类型为List<T> ,则可以:

clLibrary = clLibrary.Where(x => x.LastSyncToWebsite < x.Modified).ToList();

If clLibrary is of type T[] , you can: 如果clLibrary的类型为T[] ,则可以:

clLibrary = clLibrary.Where(x => x.LastSyncToWebsite < x.Modified).ToArray();

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

相关问题 从另一个集合中的一个集合中删除项目 - Deleting items from one collection in another collection 有没有办法通过数据注释来验证一个日期属性是否大于或等于另一个日期属性? - Is there a way through data annotations to verify that one date property is greater than or equal to another date property? 从另一个列表中删除项目 - Remove items from one list in another Linq从一个集合中删除元素到另一个集合 - Linq remove elements from one collection into another collection 如何从具有相同属性值的列表中删除项(计数大于2) - How to remove Items from a list with the same property value, where the count is greater than 2 反应性扩展 - 将物品从一个集合泵送到另一个集合 - Reactive Extensions - Pumping items from one collection to another 从集合中查找和删除项目 - Find And Remove Items From Collection 如果项目包含来自另一个列表的字符串,则从一个列表中删除它们 - Remove items from one list if they contain strings from another list 从另一个集合的内容过滤项目集合 - Filtering a collection of items from contents of another collection 从另一个项目中删除一个项目数组的最快方法是什么? - What is the quickest way to remove one array of items from another?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM