title: Split a list of objects into sub-lists of contiguous elements using LINQ?
body: <p>I have a simple class <code>Item</code>:</p>
  <pre><code>public class Item
  {
    public int Start { get; set;}
    public int Stop { get; set;}
  }
  </code></pre>
  <p>Given a <code>List&lt;Item&gt;</code> I want to split this into multiple sublists of contiguous elements. e.g. a method</p>
  <pre><code>List&lt;Item[]&gt; GetContiguousSequences(Item[] items)
  </code></pre>
  <p>Each element of the returned list should be an array of <code>Item</code> such that <code>list[i].Stop == list[i+1].Start</code> for each element</p>
  <p>e.g.</p>
  <pre><code>{[1,10], [10,11], [11,20], [25,30], [31,40], [40,45], [45,100]} 
  </code></pre>
  <p>=&gt;</p>
  <pre><code>{{[1,10], [10,11], [11,20]}, {[25,30]}, {[31,40],[40,45],[45,100]}}
  </code></pre>
  <p>Here is a simple (and not guaranteed bug-free) implementation that simply walks the input data looking for discontinuities:</p>
  <pre><code>List&lt;Item[]&gt; GetContiguousSequences(Item []items)
  {
          var ret = new List&lt;Item[]&gt;();
  
          var i1 = 0;
          for(var i2=1;i2&lt;items.Length;++i2)
          {
              //discontinuity
              if(items[i2-1].Stop != items[i2].Start)
              {
                  var num = i2 - i1;
                  ret.Add(items.Skip(i1).Take(num).ToArray());
                  i1 = i2;
              }
          }
          //end of array
          ret.Add(items.Skip(i1).Take(items.Length-i1).ToArray());
  
          return ret;
  }
  </code></pre>
  <p>It's not the most intuitive implementation and I wonder if there is a way to have a neater LINQ-based approach. I was looking at <code>Take</code> and <code>TakeWhile</code> thinking to find the indices where discontinuities occur but couldn't see an easy way to do this.</p>
  <p>Is there a simple way to use <code>IEnumerable</code> LINQ algorithms to do this in a more descriptive (not necessarily performant) way?</p>
  <p>I set of a simple test-case here: <a href="https://dotnetfiddle.net/wrIa2J" rel="nofollow noreferrer">https://dotnetfiddle.net/wrIa2J</a></p>