Avatar of the_7th_king
the_7th_king
Flag 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.
C#ASP.NET.NET Programming

Avatar of undefined
Last Comment
the_7th_king

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
kaufmed

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
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?
kaufmed

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.
the_7th_king

ASKER
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!
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
Ravi Vaddadi

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;
        }
    }
the_7th_king

ASKER
thanks!
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.