Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

C# comparing 2 lists of objects while ignoring the key field

I am trying to compare 2 lists of objects for equality, but I want to ignore the key field.
How can I do this?

Here is an example:

public class myProduct
{
    int ID {get; set;}
    int field1 {get; set;}
    int field2 {get; set;}
}

<List>myProduct greenProducts = new <List>myProduct();
<List>myProduct purpleProducts = new <List>myProduct();

greenProducts.Add(new myProduct { ID = 1, field1 = 5, field2 = 8});
greenProducts.Add(new myProduct { ID = 2, field1 = 9, field2 = 12});
greenProducts.Add(new myProduct { ID = 3, field1 = 7, field2 = 7});

purpleProducts.Add(new myProduct { ID = 8, field1 = 5, field2 = 8});
purpleProducts.Add(new myProduct { ID = 9, field1 = 9, field2 = 12});
purpleProducts.Add(new myProduct { ID = 10, field1 = 7, field2 = 7});

If you ignore the key field (aka the ID field), these lists are exactly the same.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

So I want my code to do something like this:

if (purpleProducts == greenProducts) //I WANT THE ID FIELD IGNORED FOR THE COMPARISON SO RESULT WITH ABOVE DATA WOULD BE TRUE
{
  //DO ACTION HERE
}

>Solution :

you can loop through it. Here is a loop that will make sure the number of duplicates are the same.

public class myProduct
{
    public int ID { get; set; }
    public int field1 { get; set; }
    public int field2 { get; set; }
}
List<myProduct> greenProducts = new List<myProduct>();
List<myProduct> purpleProducts = new List<myProduct>();

greenProducts.Add(new myProduct { ID = 1, field1 = 5, field2 = 8 });
greenProducts.Add(new myProduct { ID = 2, field1 = 9, field2 = 12 });
greenProducts.Add(new myProduct { ID = 3, field1 = 7, field2 = 7 });

purpleProducts.Add(new myProduct { ID = 8, field1 = 5, field2 = 8 });
purpleProducts.Add(new myProduct { ID = 9, field1 = 9, field2 = 12 });
purpleProducts.Add(new myProduct { ID = 10, field1 = 7, field2 = 7 });

var areEqual = true;
            
if (greenProducts.Count() != purpleProducts.Count()) areEqual = false;
if (areEqual)
{
    foreach (var item in greenProducts)
    {
        if(greenProducts.Where(g => g.field1 == item.field1 && g.field2 == item.field2).Count() != purpleProducts.Where(p => p.field1 == item.field1 && p.field2 == item.field2).Count())
        {
            areEqual = false;
            break;
        }
    }
}
if (areEqual)
{
    //DO ACTION HERE
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading