对比元素属性 [英] Comparing element properties

查看:142
本文介绍了对比元素属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以目前还比较的项目我的工作,和我收到一个列表的两个类的信息,我需要比较。我在做这个项目在C#中,它被一个MVC网页上显示。我到新的C#和完全新的Web开发。

So currently there is a comparison project I'm working on, and I am receiving a list back with the two classes of information I need to compare. I'm working on this project in C# and it is being displayed on a MVC web page. I'm newer to C# and completely new to web dev.

我知道我可以写出来的一个接一个的元素比较

I know I can write out one-by-one to compare the elements

EX:

List<ESHClass> eshlist; //This just to show that eshlist is of type ESHClass and i'm 
                               purposefully comparing two elements from the same list

     bool pNum = eshlist[0].PolicyNumber == eshlist[1].PolicyNumber;

不过,我想知道是否有任何更有效的方法来做到这一点在C#中?
我需要比较一个接一个,因为我会只显示了不同的字段。
我浏览了一下网上的,但并没有完全找到我一直在寻找。如果你有什么好的建议或文章给我一个更好的方向发展,我想AP preciate呢!

But I was wondering if there were any more efficient ways to do it in C#? I need to compare one-by-one because I'll only be displaying the fields that are different. I've browsed a bit online, but didn't quite find what I was looking for. If you have any good tips or articles to send me in a better direction, I'd appreciate it!

澄清:

我写信澄清我想用我的工具来完成的。

I'm writing to clarify what I want to accomplish with my tool.

我将有一个类类型的列表(例如ESHCLASS)

I will have a list of a class type (ex. ESHCLASS)

List<ESHClass> eshlist;

和ESHClass组成要素,如:

And ESHClass is composed of elements like:

public class ESHClass{
  public string PolicyNumber;
  public string PolicyMod;
  public string MultiPolicy;
  public string HasSwimmingPool;
};

所以说,eshlist有政策(ESHClass),并有平等的价值观:

So say eshlist has to policies(ESHClass) and there values equal:

eshlist[0].PolicyNumber= "7";
eshlist[0].PolicyMod= "00";
eshlist[0].MultiPolicy= "Yes";
eshlist[0].HasSwimmingPool= "No";

eshlist[1].PolicyNumber= "7";
eshlist[1].PolicyMod= "00";
eshlist[1].MultiPolicy= "No";
eshlist[0].HasSwimmingPool= "Yes";

所以,我想要做的是一种抽象的方式每个元素比较和存储仅对不同的那些和我的网站目前设置为一个MVC的ListView(显示它们的显示器不是我的一部分曾与麻烦)。

So what i'm trying to do is compare each element in an abstract way and store only the ones that are different and display them on my site which is currently set up as an MVC ListView (Display isn't the part I had trouble with).

所以,在我的例子情况下,网站会显示:

So in my example case, the website will show:

MultiPolicy MultiPolicy

HasSwimmingPool HasSwimmingPool

推荐答案

让我们假设你有一个类似的策略列表的内容:

Let us suppose you have a Policy list something similar to this:

        var policies = new List<Policy>
                        {
                            new Policy { PolicyNumber = "policy1" },
                            new Policy { PolicyNumber = "policy2" }
                        };
        policies.Add(policies[0]); //The list contains a duplicate element! (here policy1)

您可以这样做:

        var ignoreList = new List<Policy>();
        var distinctList = new List<Policy>();


        policies.ForEach(policy =>
                {
                    if (!ignoreList.Contains(policy)) distinctList.Add(policy);
                    if (policies.FindAll(t => t.Equals(policy)).Count > 0) ignoreList.Add(policy);
                });

甚至简单的,

var distinctPolicies = policies.Distinct() does the job!

这篇关于对比元素属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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