Linq分组取分组中的最大值,并且拿到最大值对应的id
第一种分组方式:唯一键:手机号码;
List<PhoneCallCTIPushData> phoneCallCTIPushDatas = new List<PhoneCallCTIPushData>(); PhoneCallCTIPushData phoneCallCTIPushData1 = new PhoneCallCTIPushData() { }; phoneCallCTIPushData1.Id = 119; phoneCallCTIPushData1.MobilePhoneNum = "13059310001"; phoneCallCTIPushData1.CreateDate = Convert.ToDateTime("2022-10-01"); phoneCallCTIPushDatas.Add(phoneCallCTIPushData1); PhoneCallCTIPushData phoneCallCTIPushData2 = new PhoneCallCTIPushData() { }; phoneCallCTIPushData2.Id = 1299; phoneCallCTIPushData2.MobilePhoneNum = "13059310001"; phoneCallCTIPushData2.CreateDate = Convert.ToDateTime("2022-11-02"); phoneCallCTIPushDatas.Add(phoneCallCTIPushData2); PhoneCallCTIPushData phoneCallCTIPushData13 = new PhoneCallCTIPushData() { }; phoneCallCTIPushData13.Id = 99; phoneCallCTIPushData13.MobilePhoneNum = "13059310001"; phoneCallCTIPushData13.CreateDate = Convert.ToDateTime("2022-12-02"); phoneCallCTIPushDatas.Add(phoneCallCTIPushData13); PhoneCallCTIPushData phoneCallCTIPushData3 = new PhoneCallCTIPushData() { }; phoneCallCTIPushData3.Id = 3; phoneCallCTIPushData3.MobilePhoneNum = "13059310002"; phoneCallCTIPushData3.CreateDate = Convert.ToDateTime("2022-10-03"); phoneCallCTIPushDatas.Add(phoneCallCTIPushData3); PhoneCallCTIPushData phoneCallCTIPushData4 = new PhoneCallCTIPushData() { }; phoneCallCTIPushData4.Id = 4; phoneCallCTIPushData4.MobilePhoneNum = "13059310002"; phoneCallCTIPushData4.CreateDate = Convert.ToDateTime("2022-10-04"); phoneCallCTIPushDatas.Add(phoneCallCTIPushData4); //第一轮筛选:筛选出最新日期的手机号对应的数据 var linqResult = from t1 in phoneCallCTIPushDatas group t1 by t1.MobilePhoneNum into groupTable select new { CreateDate2 = groupTable.Max(x => x.CreateDate), MobilePhoneNum2 = groupTable.Key }; List<PhoneCallCTIPushData> phoneCallCTIPushDatas2 = new List<PhoneCallCTIPushData>(); Console.WriteLine("打印准备要筛选的:"); foreach (var item in linqResult) { //第二轮筛选:匹配最新日期的手机号对应的id值 PhoneCallCTIPushData phoneCallCTIPushData = phoneCallCTIPushDatas
.Where(aa => aa.CreateDate == item.CreateDate2 && aa.MobilePhoneNum == item.MobilePhoneNum2)
.OrderByDescending(bb => bb.Id).FirstOrDefault();
phoneCallCTIPushDatas2.Add(phoneCallCTIPushData); Console.WriteLine(item.CreateDate2 + " " + item.MobilePhoneNum2 + " "); } Console.WriteLine(); Console.WriteLine("打印结果:"); foreach (var item in phoneCallCTIPushDatas2) { Console.WriteLine(item.Id + " " + item.MobilePhoneNum + " " + item.CreateDate); } Console.ReadKey();
第二种分组方式:组合唯一键:通话日期+手机号码;
List<PhoneCallCTIPushData> phoneCallCTIPushDatasAll = new List<PhoneCallCTIPushData>(); PhoneCallCTIPushData phoneCallCTIPushData1 = new PhoneCallCTIPushData() { }; phoneCallCTIPushData1.Id = 11; phoneCallCTIPushData1.CallDay = Convert.ToDateTime("2022-10-01"); phoneCallCTIPushData1.MobilePhoneNum = "13059310001"; phoneCallCTIPushData1.CreateDate = Convert.ToDateTime("2022-09-01"); phoneCallCTIPushDatasAll.Add(phoneCallCTIPushData1); PhoneCallCTIPushData phoneCallCTIPushData2 = new PhoneCallCTIPushData() { }; phoneCallCTIPushData2.Id = 12; phoneCallCTIPushData2.CallDay = Convert.ToDateTime("2022-10-01"); phoneCallCTIPushData2.MobilePhoneNum = "13059310001"; phoneCallCTIPushData2.CreateDate = Convert.ToDateTime("2022-09-05"); phoneCallCTIPushDatasAll.Add(phoneCallCTIPushData2); PhoneCallCTIPushData phoneCallCTIPushData13 = new PhoneCallCTIPushData() { }; phoneCallCTIPushData13.Id = 13; phoneCallCTIPushData13.CallDay = Convert.ToDateTime("2022-10-02"); phoneCallCTIPushData13.MobilePhoneNum = "13059310001"; phoneCallCTIPushData13.CreateDate = Convert.ToDateTime("2022-09-06"); phoneCallCTIPushDatasAll.Add(phoneCallCTIPushData13); PhoneCallCTIPushData phoneCallCTIPushData14 = new PhoneCallCTIPushData() { }; phoneCallCTIPushData14.Id = 14; phoneCallCTIPushData14.CallDay = Convert.ToDateTime("2022-10-02"); phoneCallCTIPushData14.MobilePhoneNum = "13059310001"; phoneCallCTIPushData14.CreateDate = Convert.ToDateTime("2022-09-02"); phoneCallCTIPushDatasAll.Add(phoneCallCTIPushData14); //-------------------------------------------------------------------------- PhoneCallCTIPushData phoneCallCTIPushData3 = new PhoneCallCTIPushData() { }; phoneCallCTIPushData3.Id = 15; phoneCallCTIPushData3.CallDay = Convert.ToDateTime("2022-10-01"); phoneCallCTIPushData3.MobilePhoneNum = "13059310002"; phoneCallCTIPushData3.CreateDate = Convert.ToDateTime("2022-09-03"); phoneCallCTIPushDatasAll.Add(phoneCallCTIPushData3); PhoneCallCTIPushData phoneCallCTIPushData4 = new PhoneCallCTIPushData() { }; phoneCallCTIPushData4.Id = 16; phoneCallCTIPushData4.CallDay = Convert.ToDateTime("2022-10-01"); phoneCallCTIPushData4.MobilePhoneNum = "13059310002"; phoneCallCTIPushData4.CreateDate = Convert.ToDateTime("2022-09-04"); phoneCallCTIPushDatasAll.Add(phoneCallCTIPushData4); //第一轮筛选:筛选出最新创建时间CreateDate的手机号对应的通话数据; //组合唯一键:通话日期CallDay+手机号码MobilePhoneNum; var linqResult = from t1 in phoneCallCTIPushDatasAll group t1 by new { CallDayGroup = t1.CallDay, MobilePhoneNumGroup = t1.MobilePhoneNum } into groupTable select new { //数据创建时间 CreateDate2 = groupTable.Max(x => x.CreateDate), //通话日期 CallDay2 = groupTable.Key.CallDayGroup, //手机号码 MobilePhoneNum2 = groupTable.Key.MobilePhoneNumGroup }; //第二轮筛选:匹配分组后的通话数据对应的id值; List<PhoneCallCTIPushData> phoneCallCTIPushDatasPrimary = new List<PhoneCallCTIPushData>(); Console.WriteLine("打印准备要筛选的:"); foreach (var item in linqResult) { PhoneCallCTIPushData phoneCallCTIPushData = phoneCallCTIPushDatasAll .Where(aa => aa.CreateDate == item.CreateDate2 && aa.CallDay == item.CallDay2 && aa.MobilePhoneNum == item.MobilePhoneNum2) .OrderByDescending(bb => bb.Id).FirstOrDefault(); //将能匹配上的数据插入队列; phoneCallCTIPushDatasPrimary.Add(phoneCallCTIPushData); Console.WriteLine(item.CreateDate2 + " " + item.MobilePhoneNum2 + " "); } Console.WriteLine(); Console.WriteLine("打印结果:"); foreach (var item in phoneCallCTIPushDatasPrimary) { Console.WriteLine(item.Id + " " + item.CreateDate + " " + item.MobilePhoneNum); } Console.ReadKey();