1

Is it possible to filter an ObservableCollection within a property? I have tried variations of the following:

public ObservableCollection<Worker> Workers
{
    get { return DataManager.Data.MasterWorkerList.Where(w => w.Known == true); }
    set { DataManager.Data.MasterWorkerList = value; }
}

I get an error that says:

Cannot implicitly convert type 'System.Collections.Generic.IEnumerable' to 'System.Collections.ObjectModel.ObservableCollection'. An explicit conversion exists (are you missing a cast?)

When I cast the type, the program compiles, but I get an InvalidCastException saying

Unable to cast object of type 'WhereEnumerableIterator1[AoW.Models.Worker]' to type 'System.Collections.ObjectModel.ObservableCollection1[AoW.Models.Worker]'

Is it possible to filter this way? If so, what am I missing?

Nadeem_MK
  • 7,533
  • 7
  • 50
  • 61
Jason D
  • 2,634
  • 6
  • 33
  • 67
  • 2
    possible duplicate of [how to convert IEnumerable to ObservableCollection?](http://stackoverflow.com/questions/3559821/how-to-convert-ienumerable-to-observablecollection) – Justin Pihony Jun 19 '13 at 02:35

1 Answers1

3

You will need to convert IEnumerable<Worker> to ObservableCollection<Worker>. You can do this by passing your IEnumerable into the constructor of the ObservableCollection when you create a new one as below:

 return  new ObservableCollection<Worker>(DataManager.Data.MasterWorkerList.Where(w => w.Known == true));

You can make your code more readable by defining an extension method . This can be done as:

public static ObservableCollection<T> ToObservableCollection<T>(this IEnumerable<T> source)
{
   if (source == null)
   {
       throw new ArgumentNullException("source");
   }
   return new ObservableCollection<T>(source);
}

Thusly, your code would become very simple and readable

public ObservableCollection<Worker> Workers
{
    get 
    { 
       var workerList = DataManager.Data.MasterWorkerList.Where(w => w.Known == true);
       return workerList.ToObservableCollection<Worker>();
    }
    set 
    { 
       DataManager.Data.MasterWorkerList = value;
    }
}
Bhushan Firake
  • 9,338
  • 5
  • 44
  • 79
  • Actually, for some reason this causes the UI to not update according to changes in the `ObservableColection`. – Jason D Jun 19 '13 at 06:31
  • But the answer is correct.Its further your issue..please consider putting another question for it – Bhushan Firake Jun 19 '13 at 06:32
  • Fair enough. Still though, do you have any ideas on how to use this solution and have the UI be able to update? – Jason D Jun 19 '13 at 06:34
  • Use fresh instance of `DataManager.Data.` each time – Bhushan Firake Jun 19 '13 at 06:40
  • It's a Singleton class. I made it that way so that I could access its contents throughout the entire project and to avoid having to limit data to my viewmodels. – Jason D Jun 19 '13 at 06:45
  • I just started programming a couple months ago (this is my first actual project) so I'm still learning the ropes. I was just looking for a way to store data in a centralized and easily accessible location and I didn't know of a better way to do it. – Jason D Jun 19 '13 at 06:51