Comparing 2 Lists of classes in C#

the_7th_king
the_7th_king used Ask the Experts™
on
Below are my 2 classes :

        public class CommtteeMemberData
    {
        public string officerid { get { return _officerid; } set { _officerid = officerid; } }
        private string _officerid;

        public string officername { get { return _officername; } set { _officername = officername; } }
        private string _officername;

        public string chairmanflag { get { return _chairmanflag; } set { _chairmanflag = chairmanflag; } }
        private string _chairmanflag;

        public string meetingattendancepercentage { get { return _meetingattendancepercentage; } set { _meetingattendancepercentage = meetingattendancepercentage; } }
        private string _meetingattendancepercentage;

        public string exceptionpayment { get { return _exceptionpayment; } set { _exceptionpayment = exceptionpayment; } }
        private string _exceptionpayment;
    }

    public class CommitteeData
    {
        public string committeeid { get { return _committeeid; } set { _committeeid = committeeid; } }
        private string _committeeid;

        public string committeename { get { return _committeename; } set { _committeename = committeename; } }
        private string _committeename;

        public string currencyid { get { return _currencyid; } set { _currencyid = currencyid; } }
        private string _currencyid;

        public string fees { get { return _fees; } set { _fees = fees; } }
        private string _fees;

        public string companyid { get { return _companyid; } set { _companyid = companyid; } }
        private string _companyid;

        public string formid { get { return _formid; } set { _formid = formid; } }
        private string _formid;

        public string fiscalyearid { get { return _fiscalyearid; } set { _fiscalyearid = fiscalyearid; } }
        private string _fiscalyearid;

        public List<CommtteeMemberData> memberlist = new List<CommtteeMemberData>();
    }

Open in new window


I create 2 lists of the type "CommitteeData" which I get from the client side using JSON like this :

        List<CommitteeData> orig = (List<CommitteeData>)Newtonsoft.Json.JsonConvert.DeserializeObject(ORIGJSONString, typeof(List<CommitteeData>));

        List<CommitteeData> toCompare = (List<CommitteeData>)Newtonsoft.Json.JsonConvert.DeserializeObject(JSONString, typeof(List<CommitteeData>));

Open in new window


My aim is to compare the 2 lists for additions, deleteion and editing.

The main keys of the above classes are committeeid & officerid.

NO LINQ syntax as the project is in .net 2.0

Any help is appreciated.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Glanced up at my screen and thought I had coded the Matrix...  Turns out, I just fell asleep on the keyboard.
Most Valuable Expert 2011
Top Expert 2015
Commented:
Here's a start of one way in which you could accomplish this. You'll need to flesh out the rest of HasBeenModified.

static void CompareLists(List<CommitteeData> original, List<CommitteeData> toCheck, List<CommitteeData> deletions, List<CommitteeData> additions, List<CommitteeData> modifieds)
{
    System.Collections.BitArray origfound = new System.Collections.BitArray(original.Count, false);

    foreach (CommitteeData item in toCheck)
    {
        bool found = false;

        for (int i = 0; i < original.Count; i++)
        {
            if (AreTheSameCommitteeData(original[i], item))
            {
                found = true;
                origfound.Set(i, true);

                if (HasBeenModified(original[i], item))
                {
                    modifieds.Add(item);
                }
            }
        }

        if (!found)
        {
            additions.Add(item);
        }
    }

    for (int i = 0; i < original.Count; i++)
    {
        if (!origfound.Get(i))
        {
            deletions.Add(original[i]);
        }
    }
}

static bool HasBeenModified(CommitteeData original, CommitteeData toCheck)
{
    if (!string.Equals(original.committeename, toCheck.committeename)) return true;
    if (!string.Equals(original.companyid, toCheck.companyid)) return true;
    if (!string.Equals(original.currencyid, toCheck.currencyid)) return true;
    if (!string.Equals(original.fees, toCheck.fees)) return true;
    if (!string.Equals(original.fiscalyearid, toCheck.fiscalyearid)) return true;
    if (!string.Equals(original.formid, toCheck.formid)) return true;

    return false;
}

static bool AreTheSameCommitteeData(CommitteeData original, CommitteeData toCheck)
{
    return string.Equals(original.committeeid, toCheck.committeeid);
}

Open in new window


Usage would be:

List<CommitteeData> deletions = new List<CommitteeData>();
List<CommitteeData> additions = new List<CommitteeData>();
List<CommitteeData> modifieds = new List<CommitteeData>();

CompareLists(orig, toCompare, deletions, additions, modifieds);

Open in new window


Also note:  It may just be a copy/paste error, but your set clauses are incorrect. The "thing" to the right of the equals sign in each set should be the word "value", not the private variable's name.

Author

Commented:
Excellent help!

Also required is checking of the inner CommitteeMemberData

So basically, within each committee, each "officerid" will be checked for addition or deletion, and edit will be checked on based of chairmanflag, exceptionpayment & meetingattendancepercentage

I am pretty sure this will be some sort of nested loop inside the CommitteeData loop.

Can you please with this?
ǩa̹̼͍̓̂ͪͤͭ̓u͈̳̟͕̬ͩ͂̌͌̾̀ͪf̭̤͉̅̋͛͂̓͛̈m̩̘̱̃e͙̳͊̑̂ͦ̌ͯ̚d͋̋ͧ̑ͯ͛̉Glanced up at my screen and thought I had coded the Matrix...  Turns out, I just fell asleep on the keyboard.
Most Valuable Expert 2011
Top Expert 2015

Commented:
I am pretty sure this will be some sort of nested loop inside the CommitteeData loop.
Yes. You can add this logic into the HasBeenModified method, just above the "return false;". I would suggest adding another method which returns a bool, and which iterates over the members of a CommtteeMemberData object (similar to what I did in HasBeenModified. This will give you good separation and clarity as to what each method does. You can essentially duplicate the same algorithm I used for CommitteeData when dealing with CommitteeMemberData.
Introduction to Web Design

Develop a strong foundation and understanding of web design by learning HTML, CSS, and additional tools to help you develop your own website.

Author

Commented:
Does it mean that I need to create 3 more lists of the type CommitteeMemberData for additions, deleteions & modifieds and pass them to a method similar to CompareLists?

Appreciate your help!
Implement IEquatable interface on both of your classes and you should be able to compare the objects using the Equals operator.

public class CommitteeData : IEquatable<CommitteeData>
    {
        public string committeeid { get { return _committeeid; } set { _committeeid = committeeid; } }
        private string _committeeid;

        public string committeename { get { return _committeename; } set { _committeename = committeename; } }
        private string _committeename;

        public string currencyid { get { return _currencyid; } set { _currencyid = currencyid; } }
        private string _currencyid;

        public string fees { get { return _fees; } set { _fees = fees; } }
        private string _fees;

        public string companyid { get { return _companyid; } set { _companyid = companyid; } }
        private string _companyid;

        public string formid { get { return _formid; } set { _formid = formid; } }
        private string _formid;

        public string fiscalyearid { get { return _fiscalyearid; } set { _fiscalyearid = fiscalyearid; } }
        private string _fiscalyearid;

        public List<CommtteeMemberData> memberlist = new List<CommtteeMemberData>();

        public bool Equals(CommitteeData other)
        {
            return (this.committeeid == other.committeeid);
        }
    }

    public class CommtteeMemberData : IEquatable<CommtteeMemberData>
    {
        public string officerid { get { return _officerid; } set { _officerid = officerid; } }
        private string _officerid;

        public string officername { get { return _officername; } set { _officername = officername; } }
        private string _officername;

        public string chairmanflag { get { return _chairmanflag; } set { _chairmanflag = chairmanflag; } }
        private string _chairmanflag;

        public string meetingattendancepercentage { get { return _meetingattendancepercentage; } set { _meetingattendancepercentage = meetingattendancepercentage; } }
        private string _meetingattendancepercentage;

        public string exceptionpayment { get { return _exceptionpayment; } set { _exceptionpayment = exceptionpayment; } }
        private string _exceptionpayment;

        public bool Equals(CommtteeMemberData other)
        {
            return this.officerid == other.officerid;
        }
    }

Author

Commented:
thanks!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial