Click here to Skip to main content
15,669,230 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello All,

I am stuck in writing a logic where I have to compare two list values and record the differences and similarities between them. These List values are bound to the Drop Down List in a web page.

I need to trace whether any values are modified/Removed/Added by the user.

For an example:
List<objx> lstObj1;

Objx Obj1= {Id1,name1,Address1}
Objx Obj2= {Id2,name2,Address2}
Objx Obj3= {Id3,name3,Address3}

All the above objects will be added in lstObj1
List<objx> lstObj2;
Objx Obj4= {Id1,name4,Address4}
Objx Obj5= {Id5,name5,Address5}
Objx Obj6= {Id6,name6,Address6}

All the above objects will be added to list 2.

Now I want the output as below..

Name has been changed for Id1,Id5 and Id6 are added by the user.

Could you please any one help me in providing some logic hints to me.

[edit]Code block added - OriginalGriff[/edit]
Updated 4-May-14 4:14am
Rahul VB 4-May-14 10:15am    
I am sorry Griff i too updated the question a little bit.
Rahul VB 4-May-14 10:18am    
I have one question: firstly you bind the lists to 2 drop down lists, and if the results change you need to display them?
Emre Ataseven 4-May-14 10:23am    
IDs are not changed, right?
Prashant Bangaluru 4-May-14 23:47pm    
ITs like from the web page user can add the id's and choose a name and address for it.. Initially it will be loaded with some values and our job is if user changes the details or adds new details /removes it.. I should store his activity into daatabse .. Id'd are identity in DB.. Please let us know if you need any furher info.
Sergey Alexandrovich Kryukov 4-May-14 13:30pm    
This is not related to programming. You should first define what do you consider a "difference" or "similarity". This problem is the most non-trivial. So far, the question does not have strict sense.

1 solution

Not a good implemantation, but maybe could give you hint.

public class Objx
    private int _id;
    private string _name;
    private string _address;

    public Objx(int id, string name, string address)
        _id = id;
        _name = name;
        _address = address;

    public int Id
        get { return _id; }
        set { _id = value; }

    public string Name
        get { return _name; }
        set { _name = value; }

    public string Address
        get { return _address; }
        set { _address = value; }

    public string Compare(Objx target)
        if (this._id == target.Id)
            var sb = new StringBuilder();
            if (this._name != target.Name)
                sb.AppendLine("For ID: " + this.Id + " Name changed: " + this._name);

            if (this.Address != target.Address)
                sb.AppendLine("For ID: " + this.Id + " Address changed: " + this.Address);

            return sb.ToString();
        return "different items";


var lstObj1 = new List<Objx>();

var obj1 = new Objx(1, "name1", "Address1");
var obj2 = new Objx(2, "name2", "Address2");
var obj3 = new Objx(3, "name3", "Address3");

lstObj1.AddRange(new List<Objx> { obj1, obj2, obj3 });

var lstObj2 = new List<Objx>();

var obj4 = new Objx(1, "name4", "Address4");
var obj5 = new Objx(5, "name5", "Address5");
var obj6 = new Objx(6, "name6", "Address6");

lstObj2.AddRange(new List<Objx> { obj4, obj5, obj6 });

var foundItems = new List<Objx>();
var sb = new StringBuilder();
foreach (var obj in lstObj2)
    var found = lstObj1.Find(p => p.Id == obj.Id);
    if (found == null)
        sb.AppendLine("ID: " + obj.Id + " " + obj.Name + " is added.");


lstObj1.Except(foundItems).ToList().ForEach(p => sb.AppendLine("ID: " + p.Id + " " + p.Name + " removed."));
Share this answer
Prashant Bangaluru 5-May-14 3:16am    
Hi Ataseven, This solution is what i expected .. Thanks for your help ..
Emre Ataseven 5-May-14 3:21am    
You're welcome

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900