Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: ASP.NET C#3.5
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]
Posted 4-May-14 3:35am
Edited 4-May-14 5:14am
Rahul VB4.7K
v3
Comments
Rahul VB at 4-May-14 10:15am
   
I am sorry Griff i too updated the question a little bit.
Rahul VB at 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 at 4-May-14 10:23am
   
IDs are not changed, right?
Prashant Bangaluru at 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 at 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.
—SA

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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";
    }
}
 
 
Then;
 
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.");
    }
    else
    {
        sb.AppendLine(obj.Compare(found));
        foundItems.Add(found);
 
    }
}
 
lstObj1.Except(foundItems).ToList().ForEach(p => sb.AppendLine("ID: " + p.Id + " " + p.Name + " removed."));
richTextBox1.AppendText(sb.ToString());
  Permalink  
Comments
Prashant Bangaluru at 5-May-14 3:16am
   
Hi Ataseven, This solution is what i expected .. Thanks for your help ..
Emre Ataseven at 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)

  Print Answers RSS
0 Mathew Soji 334
1 Maciej Los 295
2 BillWoodruff 290
3 Afzaal Ahmad Zeeshan 257
4 DamithSL 225
0 OriginalGriff 6,369
1 Sergey Alexandrovich Kryukov 5,973
2 DamithSL 5,183
3 Manas Bhardwaj 4,673
4 Maciej Los 4,045


Advertise | Privacy | Mobile
Web01 | 2.8.1411019.1 | Last Updated 4 May 2014
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100