List去重的实现
[__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
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();