C# lambda 去除重复GroupBy 查找最大记录一条数据

public class Student
        {
            public int ID { get; set; }

            public string Name { get; set; }

            public int Age { get; set; }

            public DateTime CreateTime { get; set; }
        }
        static void Main(string[] args)
        {
            List<Student> lsStudent = new List<Student>()
            {
                new Student { ID = 1 ,Name = "小例子", Age = 25, CreateTime = Convert.ToDateTime("2021-06-29") },
                new Student { ID = 2, Name = "小例子", Age = 18, CreateTime = Convert.ToDateTime("2021-06-23") },
                new Student { ID = 2, Name = "大傻逼", Age = 13, CreateTime = Convert.ToDateTime("2021-06-28") },
                new Student { ID = 2, Name = "小涉笔", Age = 23, CreateTime = Convert.ToDateTime("2021-06-21")},
                new Student { ID = 1, Name = "大师傅", Age = 22, CreateTime = Convert.ToDateTime("2021-06-27") }
            };

            var luocore = lsStudent.GroupBy(x => x.ID).Select(s => s.OrderByDescending(x => x.CreateTime).FirstOrDefault()).ToList();
            Console.ReadKey();
        }

 

posted @ 2021-06-29 19:17  LuoCore  阅读(904)  评论(0编辑  收藏  举报