Help Comparing Objects

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

36 Replies - 1263 Views - Last Post: 13 December 2018 - 08:29 PM

#1 fearfulsc2   User is offline

  • D.I.C Regular

Reputation: 15
  • View blog
  • Posts: 254
  • Joined: 25-May 16

Help Comparing Objects

Posted 10 December 2018 - 09:04 AM

Hello everyone, I am struggling to find a way to compare two objects to see if they are equal or not depending on their property name/fields and then their corresponding value.

There are 3 objects

Object 1
-> Gets a list of fields from the DB and each row will have a field value such as FirstName/LastName and whether we want to compare it or not (so either a 1 or a 0)

List Object 2
-> A list of this object is returned that will have Properties such as FirstName, LastName, Address, etc....

List Object 3
-> A List of this object is returned that will have Properties such as FirstName, LastName, Address, etc....

Object 2 and Object 3 can be of different sizes.

The goal is to compare both objects and to see which objects in the list match and which ones do not.

If Object 2 has a list of 5 items and Object 3 has a list of 4 items, we need to compare to see if any of those items in both lists match. And we compare by the individual field from Object 1. So if Object 1 has a value that says "FirstName", we will look at the Property Value of Object 2 and Object 3 that is called FirstName and we will check to see if the list of 5 from Object 2 and the list of 4 from Object 3 have any matching FirstNames.

I am really perplex by this because I started off with trying to use reflection and I was able to get the property names based off the field value but then I didn't know how to compare the two other list of objects and check for any matches.

My other concern is how could I make this efficient since the process seems to be very linear to me and I would have to compare one item from one object to each item in the other and then iterate to the next and keep doing it until all have been defined as a match or no match.

Any help/guidance?

Is This A Good Question/Topic? 0
  • +

Replies To: Help Comparing Objects

#2 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 6969
  • View blog
  • Posts: 23,685
  • Joined: 05-May 12

Re: Help Comparing Objects

Posted 10 December 2018 - 09:07 AM

Wouldn't the fact that two different objects are different types already be a indicator that they are different?

Or are you trying to do duck-typing where if the "shape" of the objects are the same, then they must be the same object even if you two different types?
Was This Post Helpful? 0
  • +
  • -

#3 modi123_1   User is offline

  • Suitor #2
  • member icon



Reputation: 15114
  • View blog
  • Posts: 60,481
  • Joined: 12-June 08

Re: Help Comparing Objects

Posted 10 December 2018 - 09:12 AM

So you are comparing object list 2 and 3?

LINQ should be of descent help.
Was This Post Helpful? 0
  • +
  • -

#4 fearfulsc2   User is offline

  • D.I.C Regular

Reputation: 15
  • View blog
  • Posts: 254
  • Joined: 25-May 16

Re: Help Comparing Objects

Posted 10 December 2018 - 09:13 AM

We are comparing 2 objects of different types but they have the same exact properties

For example
public sealed class HighSchoolStudent()
{
 public string FirstName { get; set; }
 public string LastName {get; set; }
}

public sealed class MiddleSchoolStudent()
{
 public string FirstName { get; set; }
 public string LastName { get; set; }
}

public sealed class Fields()
{
 public string FieldCode { get; set; }
 public string FieldName { get; set; }
}



The HighSchoolStudent object may return a list of 5 students while MiddleSchoolStudent returns a list 4 students.

The Fields object will have Fields returned from the db such as
* FirstName, First Name
* LastName, Last Name

So when the Code is FirstName, I want to compare the FirstName property from HighSchoolStudent and MiddleSchoolStudent. I can use reflection for that I believe.

The challenge is comparing the two lists for similarities if any and how to make it efficient since there will be a lot of looping for each field and then through each item in the lists.

And then it gets more complex with other fields such as checking for phone numbers to see if they are exactly identical but then if they are not, take out all special characters and extensions and see if they match then and if they do, do something else. If not, they don't match.

This post has been edited by fearfulsc2: 10 December 2018 - 09:15 AM

Was This Post Helpful? 0
  • +
  • -

#5 modi123_1   User is offline

  • Suitor #2
  • member icon



Reputation: 15114
  • View blog
  • Posts: 60,481
  • Joined: 12-June 08

Re: Help Comparing Objects

Posted 10 December 2018 - 09:15 AM

Any particular reason there wouldn't be a structure of 'class student' and high school/middle school inherit from that?
Was This Post Helpful? 0
  • +
  • -

#6 fearfulsc2   User is offline

  • D.I.C Regular

Reputation: 15
  • View blog
  • Posts: 254
  • Joined: 25-May 16

Re: Help Comparing Objects

Posted 10 December 2018 - 09:19 AM

View Postmodi123_1, on 10 December 2018 - 09:15 AM, said:

Any particular reason there wouldn't be a structure of 'class student' and high school/middle school inherit from that?


Not particularly sure other than one system is going to be sending us objects and we will need to compare if we have the same values they send us. If not, we need to keep note of that.

Let's say we have HighSchoolStudents and they send us MiddleSchoolStudents, we need to see if we have them in our system or not and compare/match them. If not, we need to do something else.
Was This Post Helpful? 0
  • +
  • -

#7 modi123_1   User is offline

  • Suitor #2
  • member icon



Reputation: 15114
  • View blog
  • Posts: 60,481
  • Joined: 12-June 08

Re: Help Comparing Objects

Posted 10 December 2018 - 09:26 AM

It all seems.. weirdly setup.

One class called 'student'. Nothing particularly indicates there would be different column types in a database from a 'middle school' vs 'high school'

LINQ is a great way of handling it..
    class Student
    {
        public string FName { get; set; }
    }

            List<Student> aaa = new List<Student>();
            List<Student> bbb = new List<Student>();

            aaa.Add(new Student() { FName = "asdf" });
            aaa.Add(new Student() { FName = "cvn" });
            aaa.Add(new Student() { FName = "123" });

            bbb.Add(new Student() { FName = "cvn" });
            bbb.Add(new Student() { FName = "123" });

            List<Student> foo = (from x in aaa
                                 join y in bbb on x.FName equals y.FName
                       select x).ToList<Student>();


            foreach (Student student in foo)
            {
                Console.WriteLine(student.FName);
            }

https://docs.microso...rds/join-clause


.. though in my mind you have a DB of all the students. You would get your data object from.. where ever.. and use SQL queries to verify. Names, addresses, etc.
Was This Post Helpful? 0
  • +
  • -

#8 fearfulsc2   User is offline

  • D.I.C Regular

Reputation: 15
  • View blog
  • Posts: 254
  • Joined: 25-May 16

Re: Help Comparing Objects

Posted 10 December 2018 - 09:33 AM

I was definitely considering using LINQ, but I also need to log/keep track of all the differences.

So suppose one object had a phone number of (111-222-3333)
and the other one had a phone number of (1112223333)

I have to be able to see if they match exactly first and then remove all special characters and compare again and see if they match then.

The whole process will take a bit of work I think.
Was This Post Helpful? 0
  • +
  • -

#9 modi123_1   User is offline

  • Suitor #2
  • member icon



Reputation: 15114
  • View blog
  • Posts: 60,481
  • Joined: 12-June 08

Re: Help Comparing Objects

Posted 10 December 2018 - 09:35 AM

I would strip out the hyphens and compare straight up.
Was This Post Helpful? 0
  • +
  • -

#10 fearfulsc2   User is offline

  • D.I.C Regular

Reputation: 15
  • View blog
  • Posts: 254
  • Joined: 25-May 16

Re: Help Comparing Objects

Posted 10 December 2018 - 09:39 AM

Wish I could but the tech design wants to do fuzzy comparing saying how much of a match they are.

So if they match completely identical, it's a 100% match. If we remove the special characters after the first comparison and they are now equal, they want an 80% match.
Was This Post Helpful? 0
  • +
  • -

#11 modi123_1   User is offline

  • Suitor #2
  • member icon



Reputation: 15114
  • View blog
  • Posts: 60,481
  • Joined: 12-June 08

Re: Help Comparing Objects

Posted 10 December 2018 - 09:41 AM

Why? It's an information database, right? Shouldn't be "fuzzy" about that sort of crap unless it's just a flight of fancy to eval the quality of info from some source.
Was This Post Helpful? 0
  • +
  • -

#12 Sheepings   User is offline

  • D.I.C Lover
  • member icon

Reputation: 224
  • View blog
  • Posts: 1,260
  • Joined: 05-December 13

Re: Help Comparing Objects

Posted 10 December 2018 - 09:49 AM

If the numbers are always 3, then another 3, and then four, that's nothing a foreach digit loop can't sort out with some conditional logic.

Regex might also be helpful
Was This Post Helpful? 0
  • +
  • -

#13 fearfulsc2   User is offline

  • D.I.C Regular

Reputation: 15
  • View blog
  • Posts: 254
  • Joined: 25-May 16

Re: Help Comparing Objects

Posted 10 December 2018 - 09:49 AM

I used Students as an example but in the scenario I am working on, the tech design doc has this workflow that I must adhere to. The purpose is so when the front-end user sees this information and needs to link the information or make any matches or corrections, they know how much of a match these people are.

So the way it works is,

Outside Client submits an object to us.

We put that object in a new table in our database.

We then take that object and many of the other objects they submitted to us such as (Courses, Student, Teachers, Grades, etc... -> just examples) and then compare those values to what we have in our system. We check for exact matches and similar matches, and we log how much of a match these things are from 0 to 100% or we skip the comparison if our Fields object(from above) tell us to skip the comparison for that particular property in the object.

Then we count the amount of mismatches and display from 0 to n how many mismatches there are on the front-end.
Was This Post Helpful? 0
  • +
  • -

#14 modi123_1   User is offline

  • Suitor #2
  • member icon



Reputation: 15114
  • View blog
  • Posts: 60,481
  • Joined: 12-June 08

Re: Help Comparing Objects

Posted 10 December 2018 - 10:05 AM

*shrug* Odd. I've done plenty of data entry, employee file updating, etc and never had a need to 'grade' the matching. Right - move on, wrong - fix it.

So it goes.. good luck with the LINQ and sql.
Was This Post Helpful? 0
  • +
  • -

#15 fearfulsc2   User is offline

  • D.I.C Regular

Reputation: 15
  • View blog
  • Posts: 254
  • Joined: 25-May 16

Re: Help Comparing Objects

Posted 10 December 2018 - 10:13 AM

But yes, this is the scenario I am working on and I have to make it generic since there are quite a few objects to compare, so I wanted to create a helper class to compare to see if they are an exact match and if not, return the two values and have the business logic work on those two values and see if they are a match.

The hard part is that they may be two different sizes and even if they were the same size, the data may not match 1-1 anyways and I need to match the like with the like and then display the differences for the rest.

My process of thinking was like this

*Get Object A
*Get Object B
*Get Fields (FieldName, FieldCode, Compare)


 PropertyInfo[] properties = ObjectA.GetType().GetProperties(BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance);



Compare the two objects for that property and move onto the next property. Continue until all properties are accounted for and move onto the next comparison of objects.

Continue until one of the lists have been iterated through(not efficient at all)

The other option
* Use LINQ
* Get all like/similar objects
* Remove them from list after doing more in-depth analysis for how much alike they are
* Log the rest that are not similar in any way, shape, or form.
Was This Post Helpful? 0
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3