0

I have this Azure Model called Matches where I have OrderId and MatchedOrderIdlike this

    public String OrderId
    {
        get
        {
            return this.PartitionKey;
        }
        set
        {
            this.PartitionKey = value;
        }
    }

    public String MatchedOrderId
    {
        get
        {
            return this.RowKey;
        }
        set
        {
            this.RowKey = value;
        }
    }

So when I have two orders and they match I make two entries in this table. Making two entries is so that I can charge Organizations on the number of matches they have.

Now I want to display the number of matches, what I have been doing before is Getting all the matches in this Model like this

 public static IEnumerable<Match> GetAll()
 {
    return TableHelper.GetAll<Match>();
 }

But now I want to show the actual matches. So what I want to show is just one entry for the two entries I created. After getting all the matches like this

List<Match> matches = Matches.GetAll();

Is there a Linq query that can give me what I want?

It is not the duplicate of what has mentioned, because in that the records are identical but here the Entries are different. So an example is,

Entry 1:

OrderId: "xyz"
MatchedOrderId: "abc"

Entry 2:

OrderId: "abc"
MatchedOrderId: "xyz"

If I do simple Distinct it won't work.

mohsinali1317
  • 4,255
  • 9
  • 46
  • 85
  • Possible duplicate of [Get a list of distinct values in List](http://stackoverflow.com/questions/10255121/get-a-list-of-distinct-values-in-list) – MakePeaceGreatAgain Feb 05 '16 at 06:57
  • @HimBromBeere check my explanation to see how it is not a duplicate and then if you are satisfied please remove the close tag – mohsinali1317 Feb 05 '16 at 07:04
  • So you want to handle `OrderId = "xyz"` and `MatchOrderId = "abc"` to be equal? I don´t see any rule that would correspond to this. Or do you mean as the `OrderId`are equal the two entries should be equal? Than it IS a duplicate, however maybe of another question. – MakePeaceGreatAgain Feb 05 '16 at 07:08
  • I want to see if in both the entries if(entry1.orderId == entry2.MatchedOrderId && entry2.OrderId == entry1.MatchedOrderId) if this is true then only add one of the two entries. – mohsinali1317 Feb 05 '16 at 07:10

1 Answers1

1

You need Distinct-method that has the IEqualityComparer-overload to define what equality means:

var result = matches.Distinct(new EqualityComparer());

class EqualityComparer : IEqualityComparer<Matches> {
    public bool Equals(Matches m1, Matches m2) { return m1.OrderId == m2.MatchedOrderId && m2.OrderId == m1.MatchedOrderId ; }
    public int GetHashCode(Matches m) { return 17 * m.OrderId.GetHashCode() + m.MatchOrderId.GetHashCode(); }
}

It is important to implement the Equals- and the GetHashCode-method on IEqualityComparer.

EDIT: Obviously the above hashcode is not good, so we take another one that is able to handle swappable properties:

return m.OrderId.GetHashCode() + m.MatchOrderId.GetHasCode();

The reason why we also need to implement this method is when comparing two instances before any call to Equals the hashcode for two instances is compared, which serves as some kind of pre-indicator if the instances might be equal. This might be in particular helpful when the actual check for equality has much logic and contains of comparisons for many different properties. Further read on this thread.

Community
  • 1
  • 1
MakePeaceGreatAgain
  • 35,491
  • 6
  • 60
  • 111