编程语言
首页 > 编程语言> > c# 多时间段去重

c# 多时间段去重

作者:互联网

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApp4
{
    public class Program
    {

        public static void Main()
        {
            List<TimeRange> list = AddTime();

            List<TimeRange> TimeNew = ExtractCombination(list).OrderBy(c => c.StartPoint).ThenBy(c => c.EndPoint).ToList();


            for (var i = 0; i < TimeNew.Count; i++)
            {
                Console.WriteLine(TimeNew[i].StartPoint.ToString("HH:mm:ss") + "----" + TimeNew[i].EndPoint.ToString("HH:mm:ss"));
            }
        }



        static List<TimeRange> ExtractCombination(List<TimeRange> list)
        {
            list = list.OrderByDescending(c => c.StartPoint).ThenByDescending(c => c.EndPoint).ToList();
            if (list.Count <= 1)
            {
                return list;
            }
            int num = list.Count - 1;
            for (int i = list.Count - 2; i >= 0; i--)
            {
                if (list[i].StartPoint >= list[num].StartPoint && list[i].StartPoint < list[num].EndPoint)
                {
                    if (list[i].EndPoint <= list[num].EndPoint)
                    {
                        list.Remove(list[i]);
                    }
                    else if (list[i].EndPoint > list[num].StartPoint)
                    {
                        list[i].StartPoint = list[num].EndPoint;
                    }
                }
                else if (list[i].StartPoint<= list[num].StartPoint&& list[i].EndPoint<= list[num].EndPoint)
                {
                    list.Remove(list[i]);
                }
                else if (list[i].StartPoint <= list[num].StartPoint && list[i].EndPoint > list[num].EndPoint)
                {
                    list[i].StartPoint = list[num].EndPoint;
                }
                num = num - 1;

            }

            return list;
        }

        static List<TimeRange> AddTime()
        {
            var extractListOld = new List<TimeRange>();
            extractListOld.Add(new TimeRange() { StartPoint = Convert.ToDateTime("2022-07-03 08:00:00"), EndPoint = Convert.ToDateTime("2022-07-03 10:00:00") });

            extractListOld.Add(new TimeRange() { StartPoint = Convert.ToDateTime("2022-07-03 08:00:00"), EndPoint = Convert.ToDateTime("2022-07-03 12:00:00") });
            extractListOld.Add(new TimeRange() { StartPoint = Convert.ToDateTime("2022-07-03 08:00:00"), EndPoint = Convert.ToDateTime("2022-07-03 11:00:00") });
            extractListOld.Add(new TimeRange() { StartPoint = Convert.ToDateTime("2022-07-03 11:00:00"), EndPoint = Convert.ToDateTime("2022-07-03 16:00:00") });
            extractListOld.Add(new TimeRange() { StartPoint = Convert.ToDateTime("2022-07-03 14:00:00"), EndPoint = Convert.ToDateTime("2022-07-03 18:00:00") });
            extractListOld.Add(new TimeRange() { StartPoint = Convert.ToDateTime("2022-07-03 16:00:00"), EndPoint = Convert.ToDateTime("2022-07-03 22:00:00") });

            return extractListOld;
        }

        public class TimeRange
        {
            public DateTime StartPoint { get; set; }

            public DateTime EndPoint { get; set; }
        }
    }
}

 

标签:00,EndPoint,07,c#,list,时间段,ToDateTime,StartPoint
来源: https://www.cnblogs.com/junhuang/p/16475008.html