Willem
Willem

Reputation: 81

How to compare, in C#, two lists of objects on one or more properties of these objects?

First of all I must say I'm not a seasoned programmer. I looked at similar problems on StackOverflow but didn't seem to find a suitable answer that I can implement with my limited skills.

In C#, I need to compare two lists of objects based on the values of one or more properties in those objects. I want to create two new lists, one of the objects that exist in the left, but have differences in some property values in, or don't exist at all in the right list and vice versa.

Before I only had to compare the two based on one value, so I did not have to work on objects but on string, so I did something like this:

(LeftItems and RightItems are Entities)

List<String> leftList = new List<string>();
List<String> rightList = new List<string>();
List<String> leftResultList = new List<string>();
List<String> rightResultList = new List<string>();
List<String> leftResultObjList = new List<string>();
List<String> rightResultObjList = new List<string>();


foreach (item i in leftItems)
{
  leftlist.Add(i.value);
}

//same for right 

foreach (string i in leftList)
{
  if(!rightList.contains(i))
  {
    leftResultList.Add(i);
  }
}

//same for the right list

Now I have to compare on more than one value, so I created a class which has several properties that I need to compare, so I'd like to do the same as the above, but with object properties:

class CompItems
{
  string _x;
  string _y;

  public CompItems(string x, string y)
        {
         _x = x;
         _y = y;
        }
}

foreach (item i in leftItems)
{
  leftList.Add(new CompItem(i.value1,i.value2));
}

//same for the right list

foreach (CompItem c in leftItems)
{
  // Here is where things go wrong
  if(one property of object in rightItems equals property of object in leftItems) && some other comparisons
  {
    resultLeftObjList.Add(c)
  }
}

//And the same for the right list

Upvotes: 3

Views: 9595

Answers (3)

Likurg
Likurg

Reputation: 2760

For example override

public Coordinates(string x, string y)
{
    X = x;
    Y = y;
}

public string X { get; private set; }
public string Y { get; private set; }

public override bool Equals(object obj)
{
    if (!(obj is Coordinates))
    {
        return false;
    }
    Coordinates coordinates = (Coordinates)obj;
    return ((coordinates.X == this.X) && (coordinates.Y == this.Y));
}

And then call 'Equal' of list

Upvotes: 2

GETah
GETah

Reputation: 21419

You can make your class inherit from IComparable and do the comparison based on the properties you want like the following:

    class Employee : IComparable
    {
       private string name;
       public   string Name
       {
          get { return name; }
          set { name = value ; }
       }

       public Employee( string a_name)
       {
          name = a_name;
       }

       #region IComparable Members
       public   int CompareTo( object obj)
       {
         Employee temp = (Employee)obj;
         if ( this.name.Length < temp.name.Length)
           return -1;
         else return 0;
       }
   }

You can find the details of this solution here

Upvotes: 6

Tigran
Tigran

Reputation: 62246

The easiest and most OOP approach in this case, imo, could be a simple implementation of IComparable Interface on your both types, and after simply call CompareTo.

Hope this helps.

Upvotes: 4

Related Questions