6

I have two Observable Collections that I need to merge into one collection.

This arises because I have two methods, getTasks(staffID) which returns an ObservableCollection and getTasks(teamID) which selects the staff and pulls back the staff tasks.

For the teams I will have multiple small observableCollections, I just want to merge them.

VolkerK
  • 95,432
  • 20
  • 163
  • 226
DavidA
  • 467
  • 1
  • 4
  • 14
  • "I just want to _merge_ them" - does that mean that you have to take care of duplicates? Or does a simple concatenation suffice? See http://msdn.microsoft.com/en-us/library/bb302894.aspx – VolkerK Aug 18 '09 at 13:20
  • I was not concerned with duplicates. However I've gone down the path of Union<> which is similar but provides for other design features; there is a shared task system. Thanks. – DavidA Aug 18 '09 at 13:30

3 Answers3

10

This is what worked for me:

Concat or Union, but the result is a IEnumerable, so it needs to be converted back into an ObservableCollection:

var result = new ObservableCollection<T>(list1.Concat(list2));
Samo
  • 2,093
  • 1
  • 17
  • 18
5

I found a reference to CompositeCollection on another SO article: Merged ObservableCollection

Since you're looking to do a Concat/Union, this seems to do exactly that. Too bad it's not strongly typed.

Community
  • 1
  • 1
Matt DeKrey
  • 11,582
  • 5
  • 54
  • 69
1

I went with volkerK's suggestion and then moved to:

http://msdn.microsoft.com/en-us/library/system.linq.enumerable.union.aspx Ta

DavidA
  • 467
  • 1
  • 4
  • 14