I have a collection
ObservableCollection<PathInfo> _pathInfos = new ObservableCollection<PathInfo>();
and the corresponding sorted view:
CollectionView _sortedPathInfos = CollectionViewSource.GetDefaultView(_pathInfos);
_sortedPathInfos.SortDescriptions.Add(new SortDescription("LastAccess", ListSortDirection.Descending));
Now I want to ensure that not more than _maxItems are in the source collection. The oldest items that exceed the max count shall be removed. To achieve this I have to write quite many lines of code:
private void ensureCount()
{
var removes = new List<PathInfo>();
int count = 0;
foreach (PathInfo info in _sortedPathInfos)
{
if (++count > _maxItems)
removes.Add(info);
}
foreach (var remove in removes)
_pathInfos.Remove(remove);
}
Are there better (shorter) ways to do this?