http://www.cnblogs.com/csharpx/archive/2010/05/27/1745122.html
楼主可以看看 

解决方案 »

  1.   


                List<int> ss = new List<int>();
                ss.Add(1);
                ss.Add(1);
                ss.Add(2);
                ss.Add(2);
                ss.Add(3);
                ss.Add(3);
                List<int> ss1= ss.Distinct().ToList();
                //ss1里只有1,2,3
      

  2.   

    顾名思义,唯一啊,看微软中午msdn总知道吧
      

  3.   


    List<int> ages = new List<int> { 21, 46, 46, 55, 17, 21, 55, 55 };            IEnumerable<int> distinctAges = ages.Distinct();            Console.WriteLine("Distinct ages:");            foreach (int age in distinctAges)
                {
                    Console.WriteLine(age);
                }//自定义集合要实现自定义比较器public class Product
    {
        public string Name { get; set; }
        public int Code { get; set; }
    }// Custom comparer for the Product class
    class ProductComparer : IEqualityComparer<Product>
    {
        // Products are equal if their names and product numbers are equal.
        public bool Equals(Product x, Product y)
        {        //Check whether the compared objects reference the same data.
            if (Object.ReferenceEquals(x, y)) return true;        //Check whether any of the compared objects is null.
            if (Object.ReferenceEquals(x, null) || Object.ReferenceEquals(y, null))
                return false;        //Check whether the products' properties are equal.
            return x.Code == y.Code && x.Name == y.Name;
        }    // If Equals() returns true for a pair of objects 
        // then GetHashCode() must return the same value for these objects.    public int GetHashCode(Product product)
        {
            //Check whether the object is null
            if (Object.ReferenceEquals(product, null)) return 0;        //Get hash code for the Name field if it is not null.
            int hashProductName = product.Name == null ? 0 : product.Name.GetHashCode();        //Get hash code for the Code field.
            int hashProductCode = product.Code.GetHashCode();        //Calculate the hash code for the product.
            return hashProductName ^ hashProductCode;
        }}
    Product[] products = { new Product { Name = "apple", Code = 9 }, 
                           new Product { Name = "orange", Code = 4 }, 
                           new Product { Name = "apple", Code = 9 }, 
                           new Product { Name = "lemon", Code = 12 } };//Exclude duplicates.IEnumerable<Product> noduplicates =
        products.Distinct(new ProductComparer());foreach (var product in noduplicates)
        Console.WriteLine(product.Name + " " + product.Code);/*
        This code produces the following output:
        apple 9 
        orange 4
        lemon 12
    *///以上是MSDN的示例代码,简单易读  容易上手
      

  4.   

    http://blog.csdn.net/q107770540/article/details/5784646
      

  5.   

    List<T>,时常用到,但每次都很混乱学习中,顶一下~~~
      

  6.   


    http://www.cnblogs.com/zhongweiv/archive/2011/11/11/IEqualityComparer.htmlhttp://www.cnblogs.com/ldp615/archive/2011/08/02/quickly-create-instance-of-iequalitycomparer-and-icomparer.html
      

  7.   

    多谢各位的回帖!经过各位的提示,我找到了另外一个去除重复项的方法!//实体类 
    public class Province
        {
            public string id { get; set; }
            public string name { get; set; }
        }
    //过滤
    List<Province> newprovinceSelect = provinceSelect.GroupBy(p => p.id).Select(p => new Province { id = p.Key, name = p.FirstOrDefault().name }).ToList();
      

  8.   

    先写一个类public class CommonEqualityComparer<T, V> : IEqualityComparer<T>
        {
            private Func<T, V> keySelector;
            private IEqualityComparer<V> comparer;        public CommonEqualityComparer(Func<T, V> keySelector, IEqualityComparer<V> comparer)
            {
                this.keySelector = keySelector;
                this.comparer = comparer;
            }        public CommonEqualityComparer(Func<T, V> keySelector)
                : this(keySelector, EqualityComparer<V>.Default)
            { }        public bool Equals(T x, T y)
            {
                return comparer.Equals(keySelector(x), keySelector(y));
            }        public int GetHashCode(T obj)
            {
                return comparer.GetHashCode(keySelector(obj));
            }
        }在为IEnumerable<T>类型写一个扩展方法
    public static IEnumerable<T> Distinct<T, V>(this IEnumerable<T> source, Func<T, V> keySelector)
            {
                return source.Distinct(new CommonEqualityComparer<T, V>(keySelector));
            }        public static IEnumerable<T> Distinct<T, V>(this IEnumerable<T> source, Func<T, V> keySelector, IEqualityComparer<V> comparer)
            {
                return source.Distinct(new CommonEqualityComparer<T, V>(keySelector, comparer));
            }用法:object.Distinct(p=>p.属性).ToList();