问题
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
回答1:
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.
回答2:
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.
回答3:
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()
回答4:
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!
来源:https://stackoverflow.com/questions/17706238/comparing-element-properties