filtering observable collection

entity-framework-6 linq observablecollection wpf


I'm new to wpf, Entity Framework 6 and LINQ. I'm having a data grid bounded to a CollectionViewSource, which is bounded to ObservableCollection. Simply I followed the tutorial found in and I succeed to add, update and delete from the data grid. the tutorial shows how to load a full table (i.e. category):

private void Window_Loaded(object sender, RoutedEventArgs e) 
     System.Windows.Data.CollectionViewSource categoryViewSource = 

     categoryViewSource.Source = _context.Categories.Local; 

Now my question is very simple: What if I want to load some of the categories, and not all of them? I want some users to be able to update specific categories, so the grid will load only those categories.

4/30/2015 7:57:56 AM

Accepted Answer

you need to use a filter.

categoryViewSource.View.Filter = item =>
                     Categories category = item as Categories; // hope this are your items
                     return category.Id > 10; // or put whatever condition you want

The above method would return only the categories that have Id > 10(just used Id as an example).

Also check this link:

4/30/2015 9:01:39 AM

Popular Answer

If you want to work with linq, you can try it this way:

ObservableCollection<Model> collection = new ObservableCollection<Model>();

ObservableCollection<Model> filteredCollection = new ObservableCollection<Model>(from item in collection where item.ID >0 && item.ID < 10 orderby item.ID select item);

You can bind an observablecollection directly to the ItemsSource property of a wpf datagrid, when i am not mistaken.

If you want to know more about linq, i suggest reading

Related Questions


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow