Venkat Krishna
Venkat Krishna

Reputation: 41

Compare Two Complex List of data

When I am Trying to save current list of data into database, I need to get already existing data from database, and need to compare with current list of data.

I have two lists one is PreviousList(existing data from DB) and other is CurrentList(Modified data)

    public class SoftClose
{
    public int ID = -1;
    public int AID = -1;
    public int WFID = -1;
    public string PREFIX;
    public DateTime SCDATE;
    public string STATUS;
}

enter image description here

In CurrentList I modified Prefix to D2 where ID=1 and added new row(Id=4)...

My req is

When I am trying to save CurrentList to Db,

  1. If there is any new Prefix in CurrentList that is not there in PreviousList I need to insert that new row and need to change Status to ADD for that row.

  2. I changed Prefix to D2 where Id = 1 in CurrentList. D1 is there is DB and but not in CurrentList so i need to delete it. So i need to change the status to DELETE for that record. I should not insert D2 record where id=1 becuase D2 is already there. If I changed to D5 where Id = 1 then I need to insert it because D5 is not there in DB So i need to change the status to UPDATE.

How to do this? What is the best approach to compare lists

Upvotes: 2

Views: 92

Answers (2)

Frenchy
Frenchy

Reputation: 17037

here is a solution you could try:

List<SoftClose> previousList = new List<SoftClose>(){ 
                 new SoftClose(){ID=1, Status = "NO_CHANGE",AID="19", Prefix = "D1"},
                 new SoftClose(){ID=2, Status = "NO_CHANGE",AID="20", Prefix = "D2"},
                 new SoftClose(){ID=3, Status = "NO_CHANGE",AID="21", Prefix = "D3"}
                                                 };

List<SoftClose> currentList = new List<SoftClose>(){ 
                 new SoftClose(){ID=1, Status = "NO_CHANGE",AID="19", Prefix = "D2"},
                 new SoftClose(){ID=2, Status = "NO_CHANGE",AID="20", Prefix = "D2"},
                 new SoftClose(){ID=3, Status = "NO_CHANGE",AID="21", Prefix = "D6"},
                 new SoftClose(){ID=4, Status = "NO_CHANGE",AID="22", Prefix = "D4"},
                 new SoftClose(){ID=5, Status = "NO_CHANGE",AID="22", Prefix = "D5"}
                                                 };


var addlist = currentList.Where(c => previousList.All(p => !p.ID.Equals(c.ID) && !p.Prefix.Equals(c.Prefix)));
foreach(var n in addlist)
{ 
     var index = currentList.FindIndex(p => p.Prefix.Equals(n.Prefix));
     currentList[index].Status = "ADD";
}

var updateORdeletelist = currentList.Where(c => c.Status.Equals("NO_CHANGE") && previousList.Exists(p => p.ID.Equals(c.ID) && !p.Prefix.Equals(c.Prefix)));
foreach (var n in updateORdeletelist)
{
     var index = currentList.FindIndex(p => p.Prefix.Equals(n.Prefix));
     if (previousList.FindIndex(p => p.Prefix.Equals(n.Prefix)) < 0)
          currentList[index].Status = "UPDATE";
     else
          currentList[index].Status = "DELETE";
}

foreach (var item in currentList)
{
     Console.WriteLine($"Id:{item.ID}, Desc1:{item.Prefix}, Status:{item.Status}");  
}

output

    Id:1, Desc1:D2, Status:DELETE
    Id:2, Desc1:D2, Status:NO_CHANGE
    Id:3, Desc1:D6, Status:UPDATE
    Id:4, Desc1:D4, Status:ADD
    Id:5, Desc1:D5, Status:ADD

Upvotes: 1

K4R1
K4R1

Reputation: 895

There is a tool called Side by Side SQL Comparer in C# at https://www.codeproject.com/Articles/27122/Side-by-Side-SQL-Comparer-in-C.

basic use of the component:

using (TextReader tr = new StreamReader(@"c:\1.sql"))
{
sideBySideRichTextBox1.LeftText = tr.ReadToEnd();
}
using (TextReader tr = new StreamReader(@"c:\2.sql"))
{
sideBySideRichTextBox1.RightText = tr.ReadToEnd();
}
sideBySideRichTextBox1.CompareText();

You load the left and right sides to their respective variables sideBySideRichTextBox1.LeftText and sideBySideRichTextBox1.RightText and compare them with sideBySideRichTextBox1.CompareText();

In your case the 1.sql and 2.sql would be your PreviousList and CurrentList -database files.

There is more detailed documentation at the project-site.

Upvotes: 0

Related Questions