title: Partition enumerable according to precedence
body: <p>I'm receiving an <code>IEnumerable&lt;TransactionLineDto&gt;</code>. A <code>TransactionLineDto</code> can represent either an <code>Article</code> or a <code>Discount</code> which applies on an <code>Article</code>.</p>
  
  <p>The order matters: a <code>Discount</code> applies only on the preceding articles until another discount is found.</p>
  
  <p>There can be no discount at all (only <code>TransactionLineDto</code> representing <code>Article</code>).</p>
  
  <p>There can be many discounts in one <code>IEnumerable&lt;TransactionLineDto&gt;</code>.</p>
  
  <p>There cannot be more than 1 discount applied on an article.</p>
  
  <p>Examples:</p>
  
  <ol>
  <li><code>Article1 -&gt; Article2 -&gt; Article3</code> means there are 3 articles without any discount.</li>
  <li><code>Article1 -&gt; Article2 -&gt; Discount1</code> means <code>Discount1</code> applies on both <code>Article1</code> and <code>Article2</code></li>
  <li><code>Article1 -&gt; Article2 -&gt; Discount1 -&gt; Article3 -&gt; Discount2 -&gt; Article4</code> means <code>Discount1</code> applies on <code>Article1</code> and <code>Article2</code>, <code>Discount2</code> on <code>Article3</code> and <code>Article4</code> has no discount</li>
  </ol>
  
  <p>Now I need to "partition" these sequences to be able to know which discount applies on which article (article without discount must be discarded).</p>
  
  <p>The expected output for the third example should be something like that: <code>(Discount1 -&gt; Article1, Article2), (Discount2 -&gt; Article3)</code></p>
  
  <p>How can I implement a datastructure/class that would modelize this behavior ? 
  I thought about something like that (pay attention to the <code>???</code> that I'm unable to implement yet):</p>
  
  <pre><code>public class DiscountedTransaction : ILookup&lt;TransactionLineDto, TransactionLineDto&gt;
  {
      private readonly TransactionDto _transaction;
      private readonly IEnumerable&lt;DiscountDto&gt; _allDiscounts;
      public DiscountedTransaction(TransactionDto transaction, IEnumerable&lt;DiscountDto&gt; allDiscounts)
      {
          _transaction = transaction;
          _allDiscounts = allDiscounts;
      }
      public IEnumerable&lt;TransactionLineDto&gt; this[TransactionLineDto key] =&gt; _transaction.TransactionLines
                                                                                         .Reverse&lt;TransactionLineDto&gt;()
                                                                                         .SkipWhile(l =&gt; l != key)
                                                                                         .Skip(1)
                                                                                         .TakeWhile(l =&gt; !IsDiscount(l));
      public int Count =&gt; _transaction.TransactionLines
                                      .Where(IsDiscount)
                                      .Count();
      public bool Contains(TransactionLineDto key) =&gt; _transaction.TransactionLines
                                                                  .Where(IsDiscount)
                                                                  .Contains(key);
      public IEnumerator&lt;IGrouping&lt;TransactionLineDto, TransactionLineDto&gt;&gt; GetEnumerator() =&gt; ???;
      IEnumerator IEnumerable.GetEnumerator() =&gt; GetEnumerator();
      private bool IsDiscount(TransactionLineDto line)
      {
          return _allDiscounts.Any(d =&gt; d.ID == line.Article);
      }
  }
  </code></pre>
  
  <p>Is it a good approach or is it overkill ? (I mean is it possible to do that only with the pre-existing LINQ methods ?)</p>