none
C# Collection Compare and update RRS feed

  • Question

  • Hi,

    I have two collections as example given below. I wanted to find property value wise comparistion between the collections and the different value I found in my second collection then I want to update my first collection.


    List<Student> FirstCollection = new List<Student>();
    Student a = new Student();
    a.StudentID = 1;
    a.Name = "Selva";
    Student b = new Student();
    b.StudentID = 2;
    b.Name = "kumar";
    FirstCollection.Add(a);
    FirstCollection.Add(b);
              
    List<Student> SecondCollection = new List<Student>();
    Student c = new Student();
    c.StudentID = 1;
    c.Name = "Raj";
    Student d = new Student();
    d.StudentID = 1;
    d.Name = "kumar";
    SecondCollection.Add(c);
    SecondCollection.Add(d);

    How can I implement this

    Please help me..

    Thanks


    Selvakumar Rathinam

    Thursday, August 8, 2013 7:11 AM

Answers

  • Hello Selvakumar,

    Welcome to MSDN Forum. 

    According to your description of the problem, the issue you are experiencing is how to find different students and consolidate into the first collection. If I have misunderstood anything, please feel free to let me know.

    You could try this coding below:

    SecondCollection.ForEach(s =>
                {
                    if (FirstCollection.Where(f => f.Name == s.Name).Count() == 0)
                    {
                        FirstCollection.Add(new Student() { Name = s.Name, StudentID = FirstCollection.Select(fc => fc.StudentID).Max() + 1 });
                    }
                });

    I hope this will help resolve your problem.

    Best Regards,


    Friday, August 9, 2013 4:47 AM
    Moderator
  • If you want to insert to the FirstCollections the students from SecondCollections, excluding the existing names, then try this:

    FirstCollection.AddRange( SecondCollection.Where( s2 => !FirstCollection.Select( s1 => s1.Name ).Contains( s2.Name ) ) );

    It is also possible to use custom equality comparers.

    Friday, August 9, 2013 7:03 PM
  • If I have understood your question correctly, you want to update the objects in first collection based on second collection where StudentIds match, right? If yes, here is the solution.

    FirstCollection.ForEach(first =>
        SecondCollection.ForEach(second =>
            {
                if (first.StudentID == second.StudentID && first.Name != second.Name)
                {
                    first.Name = second.Name;
                }
            }));
    I hope this helps.


    Please mark this post as answer if it solved your problem. Happy Programming!

    Monday, August 12, 2013 4:13 PM

All replies

  • Hello Selvakumar,

    Welcome to MSDN Forum. 

    According to your description of the problem, the issue you are experiencing is how to find different students and consolidate into the first collection. If I have misunderstood anything, please feel free to let me know.

    You could try this coding below:

    SecondCollection.ForEach(s =>
                {
                    if (FirstCollection.Where(f => f.Name == s.Name).Count() == 0)
                    {
                        FirstCollection.Add(new Student() { Name = s.Name, StudentID = FirstCollection.Select(fc => fc.StudentID).Max() + 1 });
                    }
                });

    I hope this will help resolve your problem.

    Best Regards,


    Friday, August 9, 2013 4:47 AM
    Moderator
  • If you want to insert to the FirstCollections the students from SecondCollections, excluding the existing names, then try this:

    FirstCollection.AddRange( SecondCollection.Where( s2 => !FirstCollection.Select( s1 => s1.Name ).Contains( s2.Name ) ) );

    It is also possible to use custom equality comparers.

    Friday, August 9, 2013 7:03 PM
  • If I have understood your question correctly, you want to update the objects in first collection based on second collection where StudentIds match, right? If yes, here is the solution.

    FirstCollection.ForEach(first =>
        SecondCollection.ForEach(second =>
            {
                if (first.StudentID == second.StudentID && first.Name != second.Name)
                {
                    first.Name = second.Name;
                }
            }));
    I hope this helps.


    Please mark this post as answer if it solved your problem. Happy Programming!

    Monday, August 12, 2013 4:13 PM