Link to home
Start Free TrialLog in
Avatar of the_7th_king
the_7th_kingFlag for India

asked on

Comparing 2 Lists of classes in C#

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.
ASKER CERTIFIED SOLUTION
Avatar of kaufmed
kaufmed
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of the_7th_king

ASKER

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?
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.
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;
        }
    }
thanks!