想要在列表中找到具有重叠时间跨度的重复项

时间:2014-02-05 16:11:09

标签: c# .net linq

public class  MyRows
{
    public string StartStop { get; set; }
    public string EndStop { get; set; }
    public TimeSpan StartTime { get; set; }
    public TimeSpan EndTime { get; set; }
    public string Day { get; set; }
}
List<MyRows> lmr = new List<MyRows>();
lmr.Add(new MyRows { StartStop = "a", EndStop = "b", StartTime = new TimeSpan(09, 00, 00), EndTime = new TimeSpan(11, 00, 00),Day=DayOfWeek.Sunday.ToString() });
lmr.Add(new MyRows { StartStop = "a", EndStop = "b", StartTime = new TimeSpan(09, 00, 00), EndTime = new TimeSpan(11, 00, 00), Day = DayOfWeek.Sunday.ToString() });
lmr.Add(new MyRows { StartStop = "a", EndStop = "b", StartTime = new TimeSpan(10, 00, 00), EndTime = new TimeSpan(12, 00, 00), Day = DayOfWeek.Tuesday.ToString() });
lmr.Add(new MyRows { StartStop = "a", EndStop = "b", StartTime = new TimeSpan(10, 30, 00), EndTime = new TimeSpan(12, 00, 00), Day = DayOfWeek.Sunday.ToString() });
lmr.Add(new MyRows { StartStop = "a", EndStop = "b", StartTime = new TimeSpan(09, 00, 00), EndTime = new TimeSpan(13, 00, 00), Day = DayOfWeek.Tuesday.ToString() });
lmr.Add(new MyRows { StartStop = "a", EndStop = "b", StartTime = new TimeSpan(09, 00, 00), EndTime = new TimeSpan(11, 00, 00), Day = DayOfWeek.Thursday.ToString() });
lmr.Add(new MyRows { StartStop = "a", EndStop = "b", StartTime = new TimeSpan(11, 00, 00), EndTime = new TimeSpan(12, 00, 00), Day = DayOfWeek.Thursday.ToString() });

我想在StartStop,EndStop,StartTime,EndTime(相同TimeSpans)和DayOfWeek

的基础上找到重复项

例如,在上面的列表“

  • 第一个对象是第二个和第四个对象(第二个bcoz 时间跨度重叠)
  • 由于时间跨度重叠,第三个对象与第五个对象重复。
  • 第六个对象没有重复,第七个时间跨度不重叠但是天相同。

1 个答案:

答案 0 :(得分:0)

这里你没有复杂性,

   var result = lmr.Where(i => lmr.Any(o => YourCondition(i,o) && i != o));

    //up to you
    private bool YourCondition(MyRow i, MyRow o)
    {
        return i.StartTime >= o.StartTime && i.EndTime <= o.EndTime && i.Day == o.Day;
    }