1

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;

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!


CLARIFICATION:

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

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

List<ESHClass> eshlist;

And ESHClass is composed of elements like:

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

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";

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 Yes MultiPolicy No

HasSwimmingPool No HasSwimmingPool Yes

juleekwin
  • 521
  • 1
  • 9
  • 25
  • I'm guessing you meant `...[0]... == ...[1]...` – Tim S. Jul 17 '13 at 17:24
  • 1
    Check this http://stackoverflow.com/questions/3669970/compare-two-listt-objects-for-equality-ignoring-order – Pavel Kutakov Jul 17 '13 at 17:24
  • 1
    @juleekwin I think this is a good question that is mistitled: it's not about the fact that they're in a list, it's about having two objects and wanting to see what properties are different. I'm not sure exactly what a better title would be. – Tim S. Jul 17 '13 at 17:25
  • I did mean ...[0]... == ...[1]..., I edited the post to reflect that. @TimS. As for the name of the post I am also unsure of what to call it, but I wish I knew so I can get it the right exposure. Anyone who thinks they have a good idea, please edit the title! :) – juleekwin Jul 17 '13 at 17:38
  • Are these supposed to be two separate lists? – Tyler Jul 17 '13 at 17:39
  • @Tyler No they are stored in the same list of class type, it was a part of the original title...I will edit my post to clarify. – juleekwin Jul 17 '13 at 17:45
  • @juleekwin: see my answer below for comparing elements of same types in a single list! – now he who must not be named. Jul 17 '13 at 18:25

4 Answers4

0

Your question is a little ambiguous as to what you are comparing. If I am interpreting it correctly you have a list and you want to get the unique values out of it. Thus you can use eshlist.Distinct() to get only the unique elements back. If you need to you can pass in a custom comparer as well so if you want to get the distinct elements by PolicyNumber you can do:

eshlist.Select(x => x.PolicyNumber).Distinct()
Brian Maupin
  • 745
  • 4
  • 17
0

Any approach that doesn't involve writing out the things one-by-one would most likely include reflection. This is better in some ways, but it's not usually my first option.

What you've got is probably the best general-purpose approach in my opinion: no magic strings, very fast performance, and rather simple to read. The only major downsides are that it'd be fairly easy to miss including a property, compare different properties, or misuse the resulting bool.

Depending on what you have to do with the knowledge of which properties are different, something like this might be good to include in the class you're comparing here:

public class MyClass {
    public IEnumerable<string> GetDifferingPropertyNames(MyClass other) { ... }
}

This could be implemented with a one-by-one approach similar to what you've got (but being contained in the class that defined the properties is better, because e.g. if you add a property, you only have to remember to change that method, not something in another file), or using reflection, e.g. something similar to the solutions in Comparing object properties in c#. But instead of only being concerned about whether the whole object is equal, list out which properties aren't equal.

Community
  • 1
  • 1
Tim S.
  • 55,448
  • 7
  • 96
  • 122
0

To get all of the properties that are different between two objects you can use reflection. Reflection can get you all of the properties of two types, let you get the value of that property for two instances of that type, and then you can compare them an indicate which properties have different values.

This method uses generics to ensure that the two instances given are of the same type, as otherwise the comparison isn't meaningful.

public static IEnumerable<PropertyInfo> GetDifferentProperties<T>(T first, T second)
{
    return typeof(T).GetProperties().Where(prop =>
        !object.Equals(prop.GetValue(first), prop.GetValue(second)));
}

From the result of this method call you can get out the Name of the property as a string, you can use it to get the values of that properties for those and other instances, and quite a lot more as well.

Servy
  • 202,030
  • 26
  • 332
  • 449
0

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)

You could do something like:

        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);
                });

Or Even simple,

var distinctPolicies = policies.Distinct() does the job!
  • This seem to me like it's checking the entire Policy to determine it's distinct. I'm looking for individual elements in the policy. So if the the policymod is different between the two elements, it just captures that to be displayed instead of the whole policy class. I added a clarification to the bottom of my post. – juleekwin Jul 17 '13 at 20:33