Unity 小技巧之 自定义List.Distinct去重

  配合lambda表达式  TypeList = TempShoppingMallItemList.Distinct(new ListComparer((p1, p2) => p1.TypeItem == p2.TypeItem)).ToList();//去重 筛选类型
using System;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;


public class ListComparer : IEqualityComparer
{
    public delegate bool EqualsComparer(F x, F y);

    public EqualsComparer equalsComparer;

    public ListComparer(EqualsComparer _euqlsComparer)
    {
        this.equalsComparer = _euqlsComparer;
    }

    public bool Equals(T x, T y)
    {
        if (null != equalsComparer)
        {
            return equalsComparer(x, y);
        }
        else
        {
            return false;
        }
    }

    public int GetHashCode(T obj)
    {
        return obj.ToString().GetHashCode();
    }
}

你可能感兴趣的:(Unity 小技巧之 自定义List.Distinct去重)