zoukankan      html  css  js  c++  java
  • List去重的实现

    List<T> 当T为值类型的时候 去重比较简单,当T为引用类型时,一般根据业务需要,根据T的中几个属性来确定是否重复,从而去重。

    查看System.Linq下的Enumerable存在一个去重方法

        /// <summary>Returns distinct elements from a sequence by using a specified <see cref="T:System.Collections.Generic.IEqualityComparer`1" /> to compare values.</summary>
            /// <param name="source">The sequence to remove duplicate elements from.</param>
            /// <param name="comparer">An <see cref="T:System.Collections.Generic.IEqualityComparer`1" /> to compare values.</param>
            /// <typeparam name="TSource">The type of the elements of <paramref name="source" />.</typeparam>
            /// <returns>An <see cref="T:System.Collections.Generic.IEnumerable`1" /> that contains distinct elements from the source sequence.</returns>
            /// <exception cref="T:System.ArgumentNullException">
            ///         <paramref name="source" /> is <see langword="null" />.</exception>
            [__DynamicallyInvokable]
            public static IEnumerable<TSource> Distinct<TSource>(this IEnumerable<TSource> source, IEqualityComparer<TSource> comparer)
            {
                if (source == null)
                {
                    throw Error.ArgumentNull("source");
                }
                return DistinctIterator(source, comparer);
            }

    通过实现IEqualityComparer<T>比较器来实现对象的比较。

    IEqualityComparer<T>的简单实现,通过委托来比较对象

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    
    namespace Extensions
    {
        public delegate bool ComparerDelegate<T>(T x, T y);
    
        /// <summary>
        /// list比较
        /// </summary>
        /// <typeparam name="T"></typeparam>
        public class ListCompare<T> : IEqualityComparer<T>
        {
            private ComparerDelegate<T> _comparer;
    
            public ListCompare(ComparerDelegate<T> @delegate)
            {
                this._comparer = @delegate;
            }
    
            public bool Equals(T x, T y)
            {
                if (ReferenceEquals(x, y))
                {
                    return true;
                }
                if (_comparer != null)
                {
                    return this._comparer(x, y);
                }
                else
                {
                    return false;
                }
            }
    
            public int GetHashCode(T obj)
            {
                return obj.ToString().GetHashCode();
            }
        }
    }

    使用方法:

    list= List.Distinct(new ListCompare<Path>
                    ((x, y) => x.Latitude == y.Latitude && x.Longitude == y.Longitude)).ToList();
  • 相关阅读:
    asp.net中分页与存储过程的一些总结
    Ajax与Json的一些总结
    aspx页面与服务器控件间运行原理
    ASP.NET中Server对象的几个方法
    Cookie与Session的一些总结
    ASP.NET的学习之asp.net整体运行机制
    Find offset of first/last found substring
    由于DNS反向解析导致的登录BI启动版速度变慢问题
    4-自定义BI启动版Logon界面
    3-自定义BI启动版是否隐藏CMS名称
  • 原文地址:https://www.cnblogs.com/tangchun/p/9894566.html
Copyright © 2011-2022 走看看