How to filter Observable Collection Class Collection

DATT OZA picture DATT OZA · Feb 20, 2010 · Viewed 9.8k times · Source

I have implemented Linq-To-Sql.. Add necessary table in it... after that linq class will automatically set property for field.. I implemented one class using ObservableCollection class.. and pass datacontextclass object in its constructor...

so after getting all data how to filter it?

  public class BindBookIssueDetails : ObservableCollection
        {
            public BindBookIssueDetails(DataClasses1DataContext dataDC)
            {
                foreach (Resource_Allocation_View res in dataDC.Resource_Allocation_Views)
                {
                    this.Add(res);
                }
            }
        }

private BindBookIssueDetails bResource;
bResource = new BindBookIssueDetails(db);
_cmbResource.ItemSource=bResource;

Please Help me.

Answer

Jobi Joy picture Jobi Joy · Feb 20, 2010

You can use CollectionViewSource and filter it. So that it affect only at the View(.XAML) side

    ICollectionView collectionView = CollectionViewSource.GetDefaultView(bResource);
    collectionView.Filter = new Predicate<object>(YourFilterFunction);

Check out this blog for more details. http://bea.stollnitz.com/blog/?p=31