关于distinct的compare参数的使用

1.创建compare类

using DCZY.Bean;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DCZY.Bean
{
    public class HookPlanMonitorDeviceCompare : IEqualityComparer<HookPlanMonitorDeviceInfo>
    {
        public bool Equals(HookPlanMonitorDeviceInfo x, HookPlanMonitorDeviceInfo y)
        {
            if (x.Device.Name == y.Device.Name)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public int GetHashCode(HookPlanMonitorDeviceInfo obj)
        {
            int hCode = obj.Device.Name.GetHashCode();
            return hCode.GetHashCode();
        }
    }
}

2.调用

List<HookPlanMonitorDevice> devicecollection = devicecollection.Distinct(new HookPlanMonitorDeviceCompare()).ToList();
萌橙 你瞅啥?
原文地址:https://www.cnblogs.com/daimaxuejia/p/11377108.html