ApiVersionSetIdComparer.cs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
using System;
using System.Collections.Generic;
using Newtonsoft.Json.Linq;
 
namespace APIManagementTemplate
{
    public class ApiVersionSetIdComparer : IEqualityComparer<JToken>
    {
        private const string Properties = "properties";
        private const string ApiVersionSetId = "apiVersionSetId";
 
        public bool Equals(JToken x, JToken y)
        {
            if (x == null && y == null)
                return true;
            if (x == null || y == null)
                return false;
            if (x[Properties] == null || y[Properties] == null)
                return false;
            return x[Properties].Value<string>(ApiVersionSetId)
                .Equals(y[Properties].Value<string>(ApiVersionSetId));
        }
 
        public int GetHashCode(JToken obj)
        {
            if (obj[Properties] == null)
                return obj.GetHashCode();
            return (obj[Properties].Value<string>(ApiVersionSetId) ?? String.Empty).GetHashCode();
        }
    }
}