Click here to Skip to main content
15,065,044 members
Please Sign up or sign in to vote.
1.80/5 (2 votes)
See more:
Hi,
I have a requirement to compare two lists and figure out the differences in it.
Say, I have List<obj> objSt and List<tempobj> objTempSt, both Obj and tempObj has same definition that is something like
class Obj
{
int client_id;
string str;
decimal d;
datetime dt;
datetime curr_dt;
decimal rd;
string name;
}

class tempObj
{
int client_id;
string str;
decimal d;
datetime dt;
datetime curr_dt;
decimal rd;
string name;
}

here is the sample data for both the lists

Obj
-----
Client_id    Str   d   dt        curr_dt    rd    name
1           Prod  2.00 01/01/01  11/19/13   5.00  Fed
2           Prod  4.50 05/03/02  12/03/13   6.00  Kev
3           Test  6.00 06/07/07  10/02/12   4.00  Sun


tempObj
-----
Client_id    Str   d   dt        curr_dt    rd    name
1           Prod  2.00 01/01/01  11/19/13   5.00  Fed
2           Wen   4.50 05/03/02  07/17/10   1.00  Tom
3           Test  5.00 03/01/07  10/02/12   4.00  Sun



I need to figure out which list element is different that is Str or d or dt or curr_dt or rd or name and whats their value Prod(in first list) and Wen(in second list) and populate the values along with list elements.

Currently I am looping through the list and comparing the values

for (int i= 0; i < Obj.Count; i++)
{
if(Obj[i].Str != tempObj[i].Str)
{
Console.Writeln("Obj Str value" + Obj[i].Str + "tempObj Str value" + tempObj[i].Str);
}
if(Obj[i].d != tempObj[i].d)
{
Console.Writeln("Obj d value" + Obj[i].d + "tempObj d value" + tempObj[i].d);
}
if(Obj[i].dt != tempObj[i].dt)
{
Console.Writeln("Obj dtvalue" + Obj[i].dt+ "tempObj dt value" + tempObj[i].dt);
}
if(Obj[i].rd != tempObj[i].rd)
{
Console.Writeln("Obj rd value" + Obj[i].rd + "tempObj rd value" + tempObj[i].rd);
}
}

It is fine for few number of records but I have huge amount of List elements and data, so I was looking for other options.
Posted
Comments
Sergey Alexandrovich Kryukov 19-Dec-13 12:59pm
   
First of all, it makes no sense in first place where you got two identical but different classes. You should never allow such things. (And why such a class name, tempObj?! It looks like you don't understand what is a class and what is a class difference.)
—SA

Despite of some absurdity of the question (please see my comment to the question to understand what I mean), some constructive advice is still possible.

Here is one of the ways: implement comparison in the item class itself (and of course, eliminate second class, use just one):
C#
class Something { // the name of the class containing "class", "object" or "obj" is ridiculous,
                  // unless this is System.Object

    //...

    decimal rd;
    string name; 

    string Compare(Something anotherInstance) { / *...* / }

    // or

    static string Compare(Something oneInstance, Something anotherInstance) { / *...* / }

}


The rationale is simple: ones you define so many fields in a class anyway, you can mention each just once for the purpose of comparison.

—SA
   
v2
First, you don’t need to define two different classes Obj and temPObj if their definitions for both are identical.
Second, you should use more descriptive name for you class and class members. It’s not the good idea to have names like str, d, rd.

Now I can try to answer your question. There is no magical solution for this if you really need to check members. So I give you only couple more advices:

1. Keep it simple. Use for loop as you are using. Linq and Enumerators will slow it down.
2. Within the loop compare first members that are most likely to differ. If you see difference no need to compare rest.
   
v2
Comments
gettgotcha 19-Dec-13 13:12pm
   
Thanks for your reply.

I was trying to explain it rather I should have put in better way and I dont use names like str, d, rd in my project I just used them for example purpose(just lazy).

I have 200 elements in the list and I need to compare each element value against other list and find out the difference and print the differences, so difference in first member will not work for me.
I was just looking to skip 200 if conditions in the for loop as I need to compare each element in the list.
Adam Zgagacz 19-Dec-13 13:20pm
   
Understood. BTW, comparing 200 list elemente will not take any significant amout of time.
gettgotcha 19-Dec-13 13:25pm
   
Yeah but against huge data as I am going to deal with nearly 100k records and I have these kind of scenarios more in future, which result in more lines of code and if there is any changes in the list elements which will result in lot of work.
Adam Zgagacz 19-Dec-13 13:31pm
   
Maybe you should rethink your solution. If both list you are comparing would contain same object instances for identical values, you would need to compare only objects, not every member in each object. It would be much faster.

Perhaps you should look at how your lists are created and populated.
gettgotcha 19-Dec-13 13:46pm
   
I can compare objects, but I need to know which element in the list is differing and their value in order to populate the differences
bowlturner 19-Dec-13 14:00pm
   
If the data is in a database (or should be) I would seriously recommend using SQL to get this info out.
Adam Zgagacz 19-Dec-13 14:10pm
   
This might be good idea.

gettgotcha, you really should re-think your solution from scratch. It must be better way than comparing huge number of list elements.
gettgotcha 19-Dec-13 14:29pm
   
Unfortunately the data is not in database.
Adam Zgagacz 19-Dec-13 14:49pm
   
It must be a better way. How your lists are populated first place?
gettgotcha 19-Dec-13 14:57pm
   
From Excel.
Adam Zgagacz 19-Dec-13 15:23pm
   
You can treat Excel as database and access data via Microsoft.Jet.OLEDB

Using SQL as suggested by bowlturner might improve performance.

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