diff --git a/src/Elastic.Clients.Elasticsearch/Core/Infer/Metric/Metrics.cs b/src/Elastic.Clients.Elasticsearch/Core/Infer/Metric/Metrics.cs index 808bc73c7cd..68cc21015f3 100644 --- a/src/Elastic.Clients.Elasticsearch/Core/Infer/Metric/Metrics.cs +++ b/src/Elastic.Clients.Elasticsearch/Core/Infer/Metric/Metrics.cs @@ -4,7 +4,6 @@ using System; using System.Collections.Generic; -using System.Linq; using Elastic.Transport; namespace Elastic.Clients.Elasticsearch; @@ -53,8 +52,8 @@ public bool Equals(Metrics other) { if (other is null) return false; - // Equality is true when the metrics names in both instances are equal, regardless of their order in the set. - return Values.OrderBy(t => t).SequenceEqual(other.Values.OrderBy(t => t)); + // Equality is true when both instances have the same metric names. + return Values.SetEquals(other.Values); } string IUrlParameter.GetString(ITransportConfiguration settings) => GetString(); @@ -71,7 +70,16 @@ private string GetString() } /// - public override int GetHashCode() => Values != null ? Values.GetHashCode() : 0; + public override int GetHashCode() + { + // Lifting the minimal target framework to .NET Standard 2.1 + // would be the best solution ever due to the HashCode type. + var hashCode = 0; + foreach (var metric in Values) + hashCode = (hashCode * 397) ^ metric.GetHashCode(); + + return hashCode; + } public static bool operator ==(Metrics left, Metrics right) => Equals(left, right); public static bool operator !=(Metrics left, Metrics right) => !Equals(left, right);