0

I'm trying to save a new object into my db and want to check if it's already in the db before saving.

And I want to check for duplicates by seeing if certain values in the objects are the same.

I have a

class Person 
{
  public string name
  public int age
  public string address
  public int zipCode
...
...
  public string createdBy
  public DateTime createdDate
}

i don't care if the createdBy and CreatedDate are the same or not, but i care about all the other values.

Instead of doing a check like this:

if (person1.name == person2.name && person1.age == person2.age &&
    person1.address == person2.address && person1.zipCode == person2.zipCode 
    ....... )
{
  //it's a duplicate
}

I am wondering if there's something like:

if (person1 == person2 [ignore createdBy && createdDate] )
{
  //it's a duplicate
}

EDIT: I have added more properties as an example because my object has about 15 properties total and I only want to compare on 13 properties instead of all 15. I know that manually checking every single property is an option but I am wondering if there is a way to compare the object minus 2 or 3 properties since that would be easier than comparing on 13 properties.

Reem A
  • 11
  • 1
  • 1
    I suggest you have a look at this answer https://stackoverflow.com/a/36125258/845655 In the `foreach` loop which iterates thru the properties you could check the property name and skip the comparison if the name is createdBy or createdDate – DeanOC Apr 25 '19 at 20:23
  • You might want to rethink storing `Age` especially if it bears on duplicates. An age of 20 for example takes in 365 different DOBs – Ňɏssa Pøngjǣrdenlarp Apr 25 '19 at 20:23
  • Possible duplicate of [Best way to compare two complex objects](https://stackoverflow.com/questions/10454519/best-way-to-compare-two-complex-objects) – Keivan Esbati Apr 25 '19 at 20:37
  • You may want to utilize a library that provides easily created comparers in code: for example, https://github.com/StephenCleary/Comparers (disclaimer: I have no connection to the author. I just like this library). – Sergey.quixoticaxis.Ivanov Apr 25 '19 at 21:06
  • @Sergey.quixoticaxis.Ivanov That is interesting, but I don't see how it helps with not manually listing all properties. And updating when new properties are added. A nice extension to that library would be autogeneration of comparers, which would be an answer to this question. – NetMage Apr 25 '19 at 22:02
  • @NetMage there are runtime comparers in the library. They do what the OP wants if it is ok to extract the list of all properties' names through reflection once (and remove the ignored properties from the resulting list): https://github.com/StephenCleary/Comparers/blob/master/doc/run-time-comparers.md I didn't use this feature though, so I can't comment on it. – Sergey.quixoticaxis.Ivanov Apr 25 '19 at 22:11
  • @Sergey.quixoticaxis.Ivanov That's what I meant - an add-on that automates that process for any type using the library as a basis. – NetMage Apr 26 '19 at 17:23

2 Answers2

0

You can make your own method to do this.

    public bool IsDuplicatePerson(Person person1, Person person2)
    {
        if (person1.name == person2.name && person1.age == person2.age) return true;
        else return false;
    }

And then call it by:

if(!IsDuplicatePerson(person1,person2))
{
     //do work
}
Icculus018
  • 1,018
  • 11
  • 19
0

I don't know if this could help you, but here's a solution:

class Person{
    public string name;
    public float age;

    public string createdBy;

    public string GetValue(){
        return (name + " " + age.ToString());
    }
}

static void Main(){
    Person p1 = new Person();
    p1.age = 0; 
    p1.name = "hello";
    p1.createdBy = "no-one";
    Person p2 = new Person();
    p2.age = 0;
    p2.name = "hello";
    p2.createdBy = "me";
    if (p1.GetValue() == p2.GetValue())
        Console.WriteLine("hello");
  • 1
    Awfully wasteful solution it is... Please don't convert objects to strings for comparison purposes which creates a lot of unnecessary strings in addition to being incorrect for types that don't have a way to serialize value to be roundtrip-able (like floats may lose precision on converting to string) – Alexei Levenkov Apr 25 '19 at 20:35
  • Well first of all: I don't see any float in his values, second of all is a good way to debug for searching for errors, and third of all he already knew how to do it without these things so I just thought for another way. –  Apr 28 '19 at 07:56