tags:

views:

35

answers:

2

Hi simple task

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

+1  A: 

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.

Stephen Cleary
thanks .I dont quite get the result i am after. ihave edited the question and clarified.I repeat hereI 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
The updated answer uses `Except` instead of `Union`.
Stephen Cleary
Hi still does not work .I should get 2 items Item 5 and 6 as they dont exists in collectionA.So the result should 2 items (5 and 6).Apology if not clear
I think I see the problem. The `Person` class uses reference equality by default. To change this behavior, you can make `Person` a `struct`, implement [value equality](http://msdn.microsoft.com/en-us/library/dd183755.aspx) in the `Person` class, or define a new type that implements `IEqualityComparer<Person>` and pass an instance of that type to `Except`.
Stephen Cleary
what about comparing the ids of the persons in both collections?Clever way of doing it?
You can define an `IEqualityComparer<Person>` that defines equality on that single field, or go with Yury's answer.
Stephen Cleary
+2  A: 

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)));
Yury Tarabanko
thanks a lot that worked as expected.Thank you
You are welcome :o)
Yury Tarabanko