0

I have Author class and to get distinct records I have implemented IEquatable:

public class Authors: IEquatable<Authors>
{
    public long ID { get; set; }
    public string Name { get; set; }


    public bool IsSelected { get; set; }
    public long DT_RowId { get; set; }
    public string ParentName { get; set; }

    public bool Equals(Authors other)
    {
        if (Name == other.Name && ID == other.ID)
        {
            return true;
        }
        return false;
    }


}

But when I try to get the distinct records, it returns all the records without filtering.

 IEnumerable<Authors> distinctData = authorsFromService.Select(c => new Authors
        {
            DT_RowId = c.DT_RowId,
            Name = c.Name,
            ID = c.ID,
            ParentName = c.ParentName ?? "",
            IsSelected = c.IsSelected
        }).Distinct();
Simsons
  • 12,295
  • 42
  • 153
  • 269
  • @John, yes indeed, Distinct uses a Set, and sets decide on duplicate using hashcode - first line of find code ` int hashCode = InternalGetHashCode(value);` – pm100 Dec 04 '18 at 01:48
  • Long story short: use the `Distinct(IEqualityComparer)` overload with a custom `IEqualityComparer` or override `Object.Equals()` and `GetHashCode()` – vzwick Dec 04 '18 at 01:52

0 Answers0