I am grouping log records by a RegEx pattern. After grouping them I'd like to get a Distinct
count of the records for each group. For this example, Distinct
is defined as the same visit key and the same year, month, day, hour, and minute.
It's just a way of getting a more accurate count of something getting logged all the way up the stack by different consumers.
Alright, so I'm grouping them like this:
var knownMessages = logRecords
.Where(record => !string.IsNullOrEmpty(record.InclusionPattern))
.GroupBy(record => new
{
MessagePattern = record.InclusionPattern
})
.Select(g => new KnownMessage
{
MessagePattern = g.Key.MessagePattern,
----> Count = g.Distinct().Count(),
Records = g.ToList()
})
.OrderByDescending(o => o.Count);
And GetHashCode
for the type is implemented like this:
public override int GetHashCode()
{
var visitKeyHash = this.VisitKey == null ?
251 : this.VisitKey.GetHashCode();
var timeHash = this.Time.Year + this.Time.Month + this.Time.Day +
this.Time.Hour + this.Time.Minute;
return ((visitKeyHash * 251) + timeHash) * 251;
}
But, for example, in the list I have three records that return the same hash code 1439926797
; I still get a count of 3
. I know it's leveraging GetHashCode
(as I expected) to do the comparison because I have a breakpoint there to see what the hash code is.
What did I miss?