Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get Count property of ICollectionView

I have ICollectionView

private ICollectionView _snapshotList;

    public ICollectionView SnapshotList {
        get { return _snapshotList; }
    }

which im setting in ViewModel constructor, where this.smapshotListModel.SnapshotItems returns ObservableCollection

_snapshotList = CollectionViewSource.GetDefaultView(this.snapshotListModel.SnapshotItems);
        _snapshotList.Filter = ErrorMsgFilter;
        _snapshotList.CollectionChanged += OnCollectionChanged;

And later in collectionChanged event im trying to get count of items in this collection,

        void OnCollectionChanged(object sender, NotifyCollectionChangedEventArgs e) {
        this.ItemCount = _snapshotList.Count();
    }

But its throwing error, that there is no Definition for Count method.

like image 419
Luboš Suk Avatar asked Dec 17 '17 10:12

Luboš Suk


1 Answers

Try like this;

_snapshotList.Cast<object>().Count();

ICollectionView implements IEnumarable but it doesn't implement IEnumerable<TSource> like List<TSource>, HashSet<TSource> etc. So, ICollectionView doesn't have a generic type and we have to cast it to IEnumerable<object> once to enable Count() method.

like image 56
lucky Avatar answered Nov 16 '22 22:11

lucky