对象列表排重和求差

1.对象列表的排重和求差。

定义一个test1类:

  public class test1
        {
            public string aa { get; set; }
            public string cc { get; set; }
        }

实现比较方法:

      public class Test1ListEquality : IEqualityComparer<test1>
        {
            public bool Equals(test1 x, test1 y)
            {
                return x.aa == y.aa && x.cc == y.cc;
            }
            public int GetHashCode(test1 obj)
            {
                return (obj == null) ? 0 : obj.ToString().GetHashCode();
            }
        }

A.用此方法可以求出差值,但性能很慢

List<test1> lstT = new List<test1>();
                List<test1> lstT2 = new List<test1>();
                for (int i = 1; i <= 100000; i++)
                {
                    test1 tt = new test1();
                    tt.cc = i.ToString();
                    tt.aa = i.ToString();
                    lstT.Add(tt);
                }
                for (int i = 1; i <= 10000; i++)
                {
                    test1 tt = new test1();
                    tt.cc = i.ToString();
                    tt.aa = i.ToString();
                    lstT2.Add(tt);
                }
                Stopwatch sw = new Stopwatch();
                sw.Start();
                var yy = lstT.Except(lstT2, new Test1ListEquality()).ToList();
//var  yy = lstT.Select(p => new { p.cc, p.aa }).Except(lstT2.Select(p => new { p.cc, p.aa })).ToList();
                sw.Stop();
                MessageBox.Show(sw.ElapsedMilliseconds.ToString());

B.Select去重,但返回值类型变了,性能提高了N倍,同样能求差。

var  yy = lstT.Select(p => new { p.cc, p.aa }).Except(lstT2.Select(p => new { p.cc, p.aa })).ToList();

2.去重

A.Test1ListEquality()方法

List<test1> lstT = new List<test1>();
for (int i = 1; i <= 100000; i++)
                {
                    test1 tt = new test1();
                    tt.cc = (i%4).ToString();
                    tt.aa = (i%4).ToString();
                    lstT.Add(tt);
                }
  Stopwatch sw = new Stopwatch();
                sw.Start();
                var yy = lstT.Distinct(new Test1ListEquality()).ToList();
//var yy = lstT.Select(p=> new { p.cc, p.aa }).Distinct().ToList();
//var yy = lstT.Where((x, i) => lstT.FindIndex(z => z.aa == x.aa && z.cc == x.cc) == i).ToList();
                sw.Stop();
                MessageBox.Show(sw.ElapsedMilliseconds.ToString());

 

B.Select去重,返回类型变了

var  yy = lstT.Select(p=> new { p.cc, p.aa }).Distinct().ToList();

C.使用lamdba表达式去重,返回值保持不变

var yy = lstT.Where((x, i) => lstT.FindIndex(z => z.aa == x.aa && z.cc == x.cc) == i).ToList();

通用去重方法:

public class Compare<T, C> : IEqualityComparer<T>
    {
        private Func<T, C> _getField;
        public Compare(Func<T, C> getfield)
        {
            this._getField = getfield;
        }
        public bool Equals(T x, T y)
        {
            return EqualityComparer<C>.Default.Equals(_getField(x), _getField(y));
        }
        public int GetHashCode(T obj)
        {
            return EqualityComparer<C>.Default.GetHashCode(this._getField(obj));
        }
    }
    public static class CommonHelper
    {
        /// <summary>
        /// 自定义Distinct扩展方法
        /// </summary>
        /// <typeparam name="T">要去重的对象类</typeparam>
        /// <typeparam name="C">自定义去重的字段类型</typeparam>
        /// <param name="source">要去重的对象</param>
        /// <param name="getfield">获取自定义去重字段的委托</param>
        /// <returns></returns>
        public static IEnumerable<T> MyDistinct<T, C>(this IEnumerable<T> source, Func<T, C> getfield)
        {
            return source.Distinct(new Compare<T, C>(getfield));
        }
    }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值