Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Filter ObservableCollection

I am very new to coding and am trying to learn to code for Windows phone. I am stuck at a problem since the last week and it is driving me crazy.

I have an ObservableCollection defined like this:

public ObservableCollection<Note> Items { get; private set; }

The Note class has two variables in it called Index and Category.

I want to filter the ObservableCollection so that I can choose a particular element from it.

Is there a simple way doing this? Any help would be highly appreciated!

like image 948
Gowtham Avatar asked Sep 05 '11 20:09

Gowtham


5 Answers

you can do:

var myCollection = GetNoteCollection(...);

var result = myCollection.Where(w => w.Category.Equals("MyCategory"));
like image 118
Davide Piras Avatar answered Nov 15 '22 02:11

Davide Piras


Update: These classes are no longer available, check out ObservableCollectionView

Original Answer
Try one of these classes: OrderedObservableCollection or FilteredObservableCollection

OriginalList = new ObservableCollection<Person>();
FilteredList = new OrderedObservableCollection<Person, int>(originalList, p => p.Age, a => a.Age >= 18);

The FilteredList contains only persons with an age >= 18 and all persons are sorted by age. The FilteredList will automatically be updated if something changes in OriginalList. If OriginalList is global and FilteredList is used on a page, you have to call FilteredList.Unload() to remove the event binding - this is needed that the garbage collector can free the FilteredList.

These classes are not fully tested, if you find a problem, please report it here.

BTW: I'm not happy with the Unload() method. Is it possible to add something like a weak event reference?

like image 34
Rico Suter Avatar answered Nov 15 '22 01:11

Rico Suter


You're not saying what you want to filter it on. But regardless, look at LINQs Enumerable.Where and Enumerable.FirstOrDefault

Example:

var answerToLifeTheUniverseAndEverything = Items.FirstOrDefault(note => note.Index == 42)
like image 45
Claus Jørgensen Avatar answered Nov 15 '22 01:11

Claus Jørgensen


I think what you are looking to use is a CollectViewSource. These are supported in Windows Phone 7.1. You can create it, wrap your ObservableCollection and set it as a bindable property in the ViewModel. I am about to try it myself. If you want to know how I get on let me know.

like image 44
NER1808 Avatar answered Nov 15 '22 03:11

NER1808


I think you should use ListCollectionView and List<> with your ObservableCollection property:

1-Binding ListCollectionView to the same data source as ObservableCollection as:

ListCollectionView lvs;
var note = from n in Note select n;
        lvs = new ListCollectionView(note.ToList <Note>());

2- create filter method:

public bool ItemIndexFilter(object obj)
    {
        Note note = obj as Note;
        return (note.Index>=10);
    }

3- Giving the Items property its filtered value:

lvs.Filter = new Predicate<object>(ItemIndexFilter);
List<Note> note_list = new List<Note>();
for (int i = 0; i < lvs.Count; i++)
     {                   
        note_list.Add((Note)lvs.GetItemAt(i));    
     }
var observe = new ObservableCollection<Note>(note_list);
items=observe;
like image 44
Bahaa Salaheldin Avatar answered Nov 15 '22 01:11

Bahaa Salaheldin