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