简体   繁体   中英

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. I know I need to use DateTime.Compare() but I'm a bit unsure how to go about it.

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 values can be compared using >= . What you need is a 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. If clLibrary is of type List<T> , you can:

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

If clLibrary is of type T[] , you can:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM