在“开始时间”和“结束时间”列表中获取重叠时间的最大值

时间:2017-04-05 07:55:09

标签: c# linq datetime intersect

我试图计算时间相交的最大值的计数。

我想要的预期结果来自下面的代码示例应为4.

总共有8次,共有6个值相交,一组4个,一组2个。

我想要得到的是交叉路口的最大值,但却无法让它发挥作用。

这是目前的代码。

void Main()
{
var times = new List<Times> {
new Times
        {
            Start = DateTime.Now,
            End = DateTime.Now.AddMinutes(10)
        },
new Times
        {
            Start = DateTime.Now,
            End = DateTime.Now.AddMinutes(10)
        },
new Times
        {
            Start = DateTime.Now.AddMinutes(2),
            End = DateTime.Now.AddMinutes(10)
        },

new Times
        {
            Start = DateTime.Now.AddMinutes(15),
            End = DateTime.Now.AddMinutes(35)
        },
new Times
        {
            Start = DateTime.Now.AddMinutes(25),
            End = DateTime.Now.AddMinutes(42)
        },
new Times
        {
            Start = DateTime.Now.AddMinutes(43),
            End = DateTime.Now.AddMinutes(50)
        },
new Times
        {
            Start = DateTime.Now.AddMinutes(55),
            End = DateTime.Now.AddMinutes(89)
        },
new Times
        {
            Start = DateTime.Now.AddMinutes(2),
            End = DateTime.Now.AddMinutes(12)
        }
};


times.OrderBy(x => x.Start);

var overlappingEvents =
                        (
                        from e1 in times
                        where times
                            .Where(e2 => e1 != e2)
                            .Where(e2 => e1.Start <= e2.End)
                            .Where(e2 => e1.End >= e2.Start)
                            .Any()
                        select e1).ToList();

overlappingEvents.OrderBy(x => x.Start);
overlappingEvents.Distinct().OrderBy(x => x.Start);
overlappingEvents.Distinct().OrderBy(x => x.Start).Count();

}

public class Times
{
public DateTime Start { get; set; }
public DateTime End { get; set; }
}

这是时间对象的输出

Start               |  End

05/04/2017 08:38:57 |  05/04/2017 08:48:57 

05/04/2017 08:38:57 |  05/04/2017 08:48:57 

05/04/2017 08:40:57 |  05/04/2017 08:48:57 

05/04/2017 08:40:57 |  05/04/2017 08:50:57 

05/04/2017 08:53:57 |  05/04/2017 09:13:57 

05/04/2017 09:03:57 |  05/04/2017 09:20:57 

05/04/2017 09:21:57 |  05/04/2017 09:28:57 

05/04/2017 09:33:57 |  05/04/2017 10:07:57 

This is the output of the overlapping object ( I have added the line where the intersect stops)

Start               |  End

05/04/2017 08:38:57 | 05/04/2017 08:48:57 

05/04/2017 08:38:57 | 05/04/2017 08:48:57 

05/04/2017 08:40:57 |  05/04/2017 08:48:57 

05/04/2017 08:40:57 |  05/04/2017 08:50:57 


---------------------------------------

05/04/2017 08:53:57 |  05/04/2017 09:13:57

05/04/2017 09:03:57 |  05/04/2017 09:20:57

如果有人能帮助我获得十字路口的最大值,我将非常感激。

由于

2 个答案:

答案 0 :(得分:2)

试试这个。如果它们至少有一个共同点(即矿石间隔的开始等于另一个区间的结束),我假设两个区间重叠。

void Main()
{
    var times = new List<Times> {
    new Times
        {
            Start = DateTime.Now,
            End = DateTime.Now.AddMinutes(10)
        },
    new Times
        {
            Start = DateTime.Now,
            End = DateTime.Now.AddMinutes(10)
        },
    new Times
        {
            Start = DateTime.Now.AddMinutes(2),
            End = DateTime.Now.AddMinutes(10)
        },
    new Times
        {
            Start = DateTime.Now.AddMinutes(15),
            End = DateTime.Now.AddMinutes(35)
        },
    new Times
        {
            Start = DateTime.Now.AddMinutes(25),
            End = DateTime.Now.AddMinutes(42)
        },
    new Times
        {
            Start = DateTime.Now.AddMinutes(43),
            End = DateTime.Now.AddMinutes(50)
        },
    new Times
        {
            Start = DateTime.Now.AddMinutes(55),
            End = DateTime.Now.AddMinutes(89)
        },
    new Times
        {
            Start = DateTime.Now.AddMinutes(2),
            End = DateTime.Now.AddMinutes(12)
        }
    };

    var overlaps = times.Select(t1 => times.Count(t2 => IsOverlapping(t1, t2))).Max();
}

bool IsOverlapping(Times t1, Times t2)
{
    if (t1.Start >= t2.Start && t1.Start <= t2.End)
    {
        return true;
    }

    if (t1.End >= t2.Start && t1.End <= t2.End)
    {
        return true;
    }

    if (t2.Start >= t1.Start && t2.Start <= t1.End)
    {
        return true;
    }

    if (t2.End >= t1.Start && t2.End <= t1.End)
    {
        return true;
    }

    return false;
}

public class Times
{
    public DateTime Start { get; set; }
    public DateTime End { get; set; }
}
如果您可以更改IsOverlapping类:

,则可以简化

Times函数

bool IsOverlapping(Times t1, Times t2)
{
    return t1.Contains(t2.Start) || t1.Contains(t2.End) || t2.Contains(t1.Start) || t2.Contains(t1.End);
}

public class Times
{
    public DateTime Start { get; set; }
    public DateTime End { get; set; }

    public bool Contains(DateTime date)
    {
        return date >= Start && date <= End;
    }
}

答案 1 :(得分:0)

再向类中添加一个属性

package javaapplication3;

    import java.io.BufferedReader;
    import java.io.InputStreamReader;
    import java.net.Authenticator;
    import java.net.PasswordAuthentication;
    import java.net.URL;
    import java.net.URLConnection;
    import java.nio.charset.Charset;

    import org.json.JSONArray;
    import org.json.JSONException;

    public class JSONREST {

        public static String callURL(String myURL) {
            System.out.println("Requested URL:" + myURL);
            StringBuilder sb = new StringBuilder();
            URLConnection urlConn = null;
            InputStreamReader in = null;
            try {
                URL url = new URL(myURL);
                urlConn = url.openConnection();
                if (urlConn != null) {
                    urlConn.setReadTimeout(60 * 1000);
                }
                if (urlConn != null && urlConn.getInputStream() != null) {
                    in = new InputStreamReader(urlConn.getInputStream(),
                            Charset.defaultCharset());
                    BufferedReader bufferedReader = new BufferedReader(in);
                    if (bufferedReader != null) {
                        int cp;
                        while ((cp = bufferedReader.read()) != -1) {
                            sb.append((char) cp);
                        }
                        bufferedReader.close();
                    }
                }
                in.close();
            } catch (Exception e) {
                throw new RuntimeException("Exception while calling URL:" + myURL, e);
            }

            return sb.toString();
        }

        public static void main(String[] args) {

            String jsonString = callURL("MY URL");
            System.out.println("\n\njsonString: " + jsonString);

            try {
                JSONArray jsonArray = new JSONArray(jsonString);
                System.out.println("\n\njsonArray: " + jsonArray);
            } 
            catch (JSONException e) {
                e.printStackTrace();
            }
        }

    }

计算差距

   public class Times
   {
       public DateTime Start { get; set; }
       public DateTime End { get; set; }
       public TimeSpan Gap { get; set; }
   }