比较两个列表对象C# [英] Comparing two list objects C#

查看:107
本文介绍了比较两个列表对象C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我需要比较两个列表并找出其中的差异。

说,我有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.

推荐答案

尽管有一些荒谬的问题(请参阅我对这个问题的评论以理解我的意思),一些建构仍然可以提供建议。



以下是其中一种方法:在项目类本身中实现比较(当然,消除第二类,只使用一个):
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):
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) { / *...* / }

}





理由很简单:无论如何你在一个类中定义了很多字段,为了比较的目的,你可以只提一次。



-SA


首先,您不需要定义两个不同的类Obj和temPObj,如果它们的定义相同。

其次,你应该为你的班级和班级成员使用更具描述性的名称。拥有像str,d,rd这样的名字并不是一个好主意。



现在我可以尝试回答你的问题了。如果你真的需要检查成员,没有神奇的解决方案。所以我只给你几个建议:



1.保持简单。在使用时使用循环。 Linq和Enumerators会降低它的速度。

2.在循环内比较最有可能不同的第一个成员。如果你看到差异,则无需比较休息。
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.


这篇关于比较两个列表对象C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆