zoukankan      html  css  js  c++  java
  • Distinct<TSource>(IEqualityComparer<TSource> comparer) 根据列名来Distinct

    1. DistinctEqualityComparer.cs

    public class DistinctEqualityComparer<T, V> : IEqualityComparer<T>
        {
            private Func<T, V> keySelector;

            public DistinctEqualityComparer(Func<T, V> keySelector)
            {
                this.keySelector = keySelector;
            }

            public bool Equals(T x, T y)
            {
                return EqualityComparer<V>.Default.Equals(keySelector(x), keySelector(y));
            }

            public int GetHashCode(T obj)
            {
                return EqualityComparer<V>.Default.GetHashCode(keySelector(obj));
            }
        }

    2. DistinctExtensions.cs

    public static class DistinctExtensions
        {
            public static IEnumerable<T> Distinct<T, V>(this IEnumerable<T> source, Func<T, V> keySelector)
            {
                return source.Distinct(new DistinctEqualityComparer<T, V>(keySelector));
            }
        }

    3. Program.cs

    class Program
        {
            static void Main(string[] args)
            {
                List<Person> personList = new List<Person>(){
                    new Person(){Id = 1, Name = "Steven"},
                    new Person(){Id = 2, Name = "Steven"},
                    new Person(){Id = 3, Name = "Steven"},
                    new Person(){Id = 3, Name = "Steven"},
                };

                List<Person> delegateList = personList.Distinct(x => x.Id).ToList();
            }
        }

  • 相关阅读:
    幸运序列(lucky) 模拟
    無名(noname) 字符串
    香港记者 图论
    Jmeter接口测试系列之参数化方法
    jmeter接口测试-总结
    python字符串
    python变量
    7.15-ROS可视化工具-标记
    6.22-Actionlib
    7.1-Move_base 参数调整
  • 原文地址:https://www.cnblogs.com/zisezhixin/p/4113930.html
Copyright © 2011-2022 走看看