C#: Modifying the list : <p>I'm having a table(date is in mm/dd/yyyy format): </p>

<pre><code>FromID  ToID    FromDate    ToDate          
S1       S2     1/1/2016    1/15/2016
S2       S3     2/1/2016    3/14/2016
S1       S2     1/5/2016    1/20/2016
S2       S3     1/25/2016   2/25/2016
S1       S2     1/21/2016   1/25/2016
</code></pre>

<p>I need to combine the rows such that the overlapping dates of the repeating fromId and ToId. For eg, for this table, the output should be:</p>

<pre><code>FromID  ToID    FromDate    ToDate          
S1       S2     1/1/2016    1/25/2016
S2       S3     1/25/2016   3/14/2016
</code></pre>

<p>I have tried the following C# code to do it but it won't work.</p>

<pre><code> public class MergeLists
  {

    static void Main(String[] args) {

        List&lt;ItemDetails&gt; theTempList = new List&lt;ItemDetails&gt;();
        List&lt;ItemDetails&gt; theOriList = new List&lt;ItemDetails&gt;();
        DateTime minDate = new DateTime(2016, 1, 1);
        DateTime maxDate = new DateTime(2016, 1, 1);
        int count;

        theOriList.Add(new ItemDetails("S1","S2", new DateTime(2016,1,1), new DateTime(2016,1,15)));
        theOriList.Add(new ItemDetails("S2", "S3", new DateTime(2016, 2, 1), new DateTime(2016, 2, 14)));
        theOriList.Add(new ItemDetails("S1", "S2", new DateTime(2016, 1, 5), new DateTime(2016, 1, 20)));
        theOriList.Add(new ItemDetails("S2", "S3", new DateTime(2016, 1, 25), new DateTime(2016, 2, 25)));
        theOriList.Add(new ItemDetails("S1", "S2", new DateTime(2016, 1, 21), new DateTime(2016, 1, 25)));




      theOriList =  theOriList.OrderBy(x =&gt; x.fromId).ThenBy(x=&gt; x.toId).ToList();

        int addnew = 0;
        for (int i = 0; i &lt; theOriList.Count; i++)
        {

            for (int j = i + 1; j &lt; theOriList.Count; j++)
            {
                if ((theOriList[i].fromId == theOriList[j].fromId) &amp;&amp; (theOriList[i].toId == theOriList[j].toId))
                {
                    if ((theOriList[i].fromDate &lt;= theOriList[j].fromDate) &amp;&amp; (theOriList[j].fromDate &lt;= theOriList[i].toDate))
                    {
                        if (theOriList[j].toDate &gt; theOriList[i].toDate)
                        {
                            maxDate = theOriList[j].toDate;
                            minDate = theOriList[i].fromDate;

                        }
                        else if (theOriList[j].toDate &lt; theOriList[i].toDate)
                        {
                            maxDate = theOriList[i].toDate;
                            minDate = theOriList[i].fromDate;

                        }

                    }

                    else if (theOriList[i].fromDate &gt; theOriList[j].fromDate)
                    {
                        if (theOriList[i].toDate &lt;= theOriList[j].toDate)
                        {
                            maxDate = theOriList[j].toDate;
                            minDate = theOriList[j].fromDate;
                        }

                        else if (theOriList[i].toDate &gt; theOriList[j].toDate)
                        {
                            maxDate = theOriList[i].toDate;
                            minDate = theOriList[j].fromDate;
                        }

                    }

                    else if ((theOriList[j].fromDate &gt; theOriList[i].toDate))
                    {
                        //Add directly
                        addnew = 1;
                    }

                }

            }

            if (addnew != 1)
            {

                theTempList.Add(new ItemDetails(theOriList[i].fromId, theOriList[i].toId, minDate, maxDate));
            }
            else if (addnew == 1)

                theTempList.Add(new ItemDetails(theOriList[i].fromId, theOriList[i].toId, theOriList[i].fromDate, theOriList[i].toDate));
        }


        theTempList = theTempList.OrderBy(x =&gt; x.fromId).ThenBy(x =&gt; x.toId).ToList();





        foreach (ItemDetails x in theTempList)
        {

            Console.WriteLine("FromId: " + x.fromId + "\tToId: " + x.toId + "\tFromDate:" + x.fromDate + "\tToDate:" + x.toDate);
        }

        Console.ReadKey();
    }


}
</code></pre>
