zoukankan      html  css  js  c++  java
  • C# List集合分组去重并保留最新的数据

     class Program
        {
            static void Main(string[] args)
            {
                List<Person> list = new List<Person>
                {
                    new Person
                    {
                        id = 1,
                        name = "李小龙",
                        date = "2021-10-21"
                    },
                     new Person
                    {
                        id = 1,
                        name = "李小龙",
                        date = "2021-11-21"
                    },
                      new Person
                    {
                        id = 2,
                        name = "李世超",
                        date = "2021-10-21"
                    }
                };
                //分组去重保留最新数据
                List<Person> newList = list.GroupBy(P => P.id).Select(P => P.OrderByDescending(P => P.date).First()).ToList();
    
                foreach (var item in newList)
                {
                    Console.WriteLine($"人员编号:{item.id} 人员名称:{item.name} 日期:{item.date}");
                }
                Console.ReadKey();
            }
    
            public class Person
            {
                /// <summary>
                /// 人员编号
                /// </summary>
                public int id { get; set; }
    
                /// <summary>
                /// 人员名称
                /// </summary>
                public string name { get; set; }
    
                /// <summary>
                /// 日期
                /// </summary>
                public string date { get; set; }
            }
        }
  • 相关阅读:
    文件权限
    文件权限
    Nginx SSL/HTTPS 配置
    Nginx SSL/HTTPS 配置
    Nginx SSL/HTTPS 配置
    安装opencv3.3.0方法
    安装opencv3.3.0方法
    安装opencv3.3.0方法
    安装opencv3.3.0方法
    阿里巴巴的体量到底有多大?
  • 原文地址:https://www.cnblogs.com/lichaoloveliangying/p/15683192.html
Copyright © 2011-2022 走看看