zoukankan      html  css  js  c++  java
  • C# List 根据对象属性去重的四种方法对比

    private void TestDistinct()
    {
        Task.Run(() =>
        {
            //生成测试数据
            DateTime dt = DateTime.Now;
            Random rnd = new Random();
            List<MyData> list = new List<MyData>();
            int total = 1000000;
            for (int i = 0; i < total; i++)
            {
                MyData info = new MyData();
                info.id = rnd.Next(1, total * 10).ToString();
                info.name = rnd.Next(1, total * 10).ToString();
                list.Add(info);
            }
            double d = DateTime.Now.Subtract(dt).TotalMilliseconds;
    
            //方法一
            DateTime dt1 = DateTime.Now;
            Dictionary<string, MyData> result1 = new Dictionary<string, MyData>();
            foreach (MyData item in list)
            {
                MyData temp;
                if (!result1.TryGetValue(item.name, out temp))
                {
                    result1.Add(item.name, item);
                }
            }
            List<MyData> r1 = result1.Values.ToList();
            double d1 = DateTime.Now.Subtract(dt1).TotalMilliseconds;
    
            //方法二
            DateTime dt2 = DateTime.Now;
            List<MyData> result2 = list.ToLookup(item => item.name).ToDictionary(item => item.Key, item => item.First()).Values.ToList();
            double d2 = DateTime.Now.Subtract(dt2).TotalMilliseconds;
    
            //方法三
            DateTime dt3 = DateTime.Now;
            List<MyData> result3 = list.Distinct(new MyCompare()).ToList();
            double d3 = DateTime.Now.Subtract(dt3).TotalMilliseconds;
    
            //方法四
            DateTime dt4 = DateTime.Now;
            List<MyData> result4 = list.GroupBy(item => item.name).Select(item => item.First()).ToList();
            double d4 = DateTime.Now.Subtract(dt4).TotalMilliseconds;
    
            this.BeginInvoke(new Action(() =>
            {
                textBox1.Text = "";
                textBox1.Text += "生成 " + list.Count.ToString("# ####") + " 条测试数据耗时:" + d + "毫秒
    
    ";
                textBox1.Text += "使用方法一去重耗时:" + d1 + "毫秒
    
    ";
                textBox1.Text += "使用ToLookup和ToDictionary去重耗时:" + d2 + "毫秒
    
    ";
                textBox1.Text += "使用Distinct去重耗时:" + d3 + "毫秒
    
    ";
                textBox1.Text += "使用GroupBy和Select去重耗时:" + d4 + "毫秒
    
    ";
                textBox1.Text += "去重后数量:" + r1.Count + "," + result2.Count + "," + result3.Count + "," + result4.Count + "" + "
    
    ";
            }));
        });
    }
     
     
    public class MyData
    {
        public string id { get; set; }
        public string name { get; set; }
    }
    
    public class MyCompare : IEqualityComparer<MyData>
    {
        public bool Equals(MyData x, MyData y)
        {
            return x.name == y.name;
        }
    
        public int GetHashCode(MyData obj)
        {
            return obj.name.GetHashCode();
        }
    }
  • 相关阅读:
    Log4net中的RollingFileAppender解析
    TortoiseSVN使用简介
    ALinq 入门学习(四)查询关键字
    ALinq 入门学习(五)删除修改数据
    ALinq 入门学习(五)插入数据
    C# 委托知识总结
    sql 分页
    C# 数据结构常用术语总结
    ALinq 入门学习(三)Where 条件查询
    ALinq 入门学习(六)Join 连接查询
  • 原文地址:https://www.cnblogs.com/qqhewei/p/13597851.html
Copyright © 2011-2022 走看看