LINQ Select Distinct с анонимными типами

public class DelegateComparer : IEqualityComparer
{
    private Func _equals;
    private Func _hashCode;
    public DelegateComparer(Func equals, Func hashCode)
    {
        _equals= equals;
        _hashCode = hashCode;
    }
    public bool Equals(T x, T y)
    {
        return _equals(x, y);
    }

    public int GetHashCode(T obj)
    {
        if(_hashCode!=null)
            return _hashCode(obj);
        return obj.GetHashCode();
    }       
}

public static class Extensions
{
    public static IEnumerable Distinct(this IEnumerable items, 
        Func equals, Func hashCode)
    {
        return items.Distinct(new DelegateComparer(equals, hashCode));    
    }
    public static IEnumerable Distinct(this IEnumerable items,
        Func equals)
    {
        return items.Distinct(new DelegateComparer(equals,null));
    }
}

var uniqueItems=students.Select(s=> new {FirstName=s.FirstName, LastName=s.LastName})
            .Distinct((a,b) => a.FirstName==b.FirstName, c => c.FirstName.GetHashCode()).ToList();

Приносим извинения за ранее anonymous-types некорректное форматирование

c#

linq

distinct

anonymous-types

equality

2022-09-22T04:14:32+00:00