user9969
user9969

Reputation: 16060

merging 2 collections and find all the unique items

I have 2 collections need to create a 3 one if you like by merging the 2 and giving me a third one with all the unique items only

              class Program
              {
                 static void Main(string[] args)
                 {

                      ObservableCollection<Person> collectionA = new ObservableCollection<Person>
                                                                  {
                                                                     new Person {Id = 1, Name = "Name1", Surname = "Surname1"},
                                                                     new Person {Id = 2, Name = "Name2", Surname = "Surname2"},
                                                                     new Person {Id = 3, Name = "Name3", Surname = "Surname3"},
                                                                     new Person {Id = 4, Name = "Name4", Surname = "Surname4"}
                                                                  };

                    ObservableCollection<Person> collectionB = new ObservableCollection<Person>
                                                                  {
                                                                     new Person {Id = 5, Name = "Name5", Surname = "Surname5"},
                                                                     new Person {Id = 2, Name = "Name2", Surname = "Surname2"},
                                                                     new Person {Id = 6, Name = "Name6", Surname = "Surname6"},
                                                                     new Person {Id = 4, Name = "Name4", Surname = "Surname4"}
                                                                  };

                    ObservableCollection<Person> result=????
                 }
              }

              public class Person
              {
                 public int Id { get; set; }
                 public string Name { get; set; }
                 public string Surname { get; set; }
              }
           }

Any ideas?Thanks a lot

EDIT CLARIFICATION

I have collectionA, then I create collection B, compare the two collection add any item to FIND ALL THE ITEMS IN COLLECTIONB THAT DONT EXISTS IN COLLECTION A AND CREATE A RESULT COLLECTION.Hope clear now

Upvotes: 1

Views: 1159

Answers (2)

Yury Tarabanko
Yury Tarabanko

Reputation: 45106

If Id is a unique identifier of you person try this one:

ObservableCollection<Person> result = new ObservableCollection<Person>(collectionB
                                        .Where(p => !collectionA.Any(p2=>p2.Id==p.Id)));

Upvotes: 2

Stephen Cleary
Stephen Cleary

Reputation: 457057

Edited answer:

ObservableCollection<Person> result = new ObservableCollection<Person>(collectionB.Except(collectionA));

Note that this will create a new collection that is not tied to the old collections - so if you add a person to collectionA, they will not show up in result automatically.

Upvotes: 1

Related Questions