title: Linq Query to join overlapping ranges and construct a new continuous range
body: <p>Hello fellows I have been trying to construct a Linq Query that finds overlapping ranges in records and constructs a new single range that will join both ranges.</p>
  
  <pre><code>public class students
  {
      public string course { get; set; }
      public int idStart { get; set;}
      public int idEnd { get; set;}
  }
  
  var c1 = new students(){course = "c#", idStart = 1, idEnd = 25};
  var j1 = new students(){course = "java",idStart = 50, idEnd = 60};
  var c2 = new students(){"c#", 20, 36};
  var j2 = new students(){"java", 40, 55};
  var c3 = new students(){"c#", 70, 80};
  
  var studentranges = new list&lt;students&gt;;
  
  studentranges.Add(c1);
  studentranges.Add(j1);
  studentranges.Add(c2);
  studentranges.Add(j2);
  studentranges.Add(c3);
  </code></pre>
  
  <p>Now I need to reformat the List <code>studentranges</code> such that reulting output is</p>
  
  <pre><code>studentranges = list&lt;"c#", 1, 36; "Java", 40, 60; "c#", 70, 80&gt;
  </code></pre>
  
  <p>Although in this example I have used only 2 ranges of C# &amp; Java. The query needs to be flexible for <code>N number of Courses and N number of Ranges</code></p>
  
  <p><strong>My Code to achieve this:</strong></p>
  
  <pre><code>var c_range = studentranges.Where(u =&gt; u.course == "c#")
                             .OrderBy(u =&gt; u.idStart)
                             .ToList();
  
  //assuming c_least is bound to exist for simplicity
  var c_least = c_range.first(); 
  
  var c_next = c_range.Where( u=&gt; u.idStart &gt; c_least.idStart &amp;&amp; u.idEnd &gt;= c_least.idEnd)
                      .First();
  
  //assuming c_next is not null
  c_least.idEnd = c_next.idEnd; 
  </code></pre>
  
  <p>How to recurs further?</p>