问题描述
我需要比较两个列表并找出其中的差异。
说,我有List< obj> objSt和List< tempobj> objTempSt,Obj和tempObj都有相同的定义,比如
class Obj
{
int client_id;
string str;
十进制d;
datetime dt;
datetime curr_dt;
decimal rd;
字符串名称;
}
class tempObj
{
int client_id;
string str;
decimal d;
datetime dt;
datetime curr_dt;
十进制rd;
字符串名称;
}
这里是两个列表的样本数据
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
我需要弄清楚哪个列表元素与Str或d或dt不同或curr_dt或rd或名称及其值是什么值Prod(在第一个列表中)和Wen(在第二个列表中)并填充值和列表元素。
目前我我循环遍历列表并比较值
for(int i = 0;我< 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);
}
}
这对于少量记录是好的,但我有大量的List元素和数据,所以我一直在寻找其他选项。
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.
推荐答案
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) { / *...* / }
}
理由很简单:无论如何你在一个类中定义了很多字段,为了比较的目的,你可以只提一次。
这篇关于比较两个列表对象C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!