title: Linq query to compose grouped collection by sum of other collection
body: <p>I have amounts that should be spent on related balances. I need to fit each amount with a proper balance. The amounts that cannot be spent because of balance is exhausted should be discarded/not selected.</p>
  <p>I have two collections:</p>
  <pre><code>var balances = new [] { 0.345, 1.234, 9.340, ... };
  var amounts = new [] { 0.011, 0.122, 1.230, 0.123, ... };
  </code></pre>
  <p>I need to group the amounts by sum up to each value in balances with this final result:</p>
  <pre><code>var result =
  {
      group1 = { 0.011, 0.122 } // 0.133 sum &lt; 0.345 balance
      group2 = { 1.230 } // 1.230 sum &lt; 1.234 balance
      group3 = { 0.123, ... } // sum &lt; 9.340 balance,
      ...
  }
  </code></pre>
  <p>Balances collection should be ordered by descending before.
  Amounts should be fit with balance by the way as it going in the collection.
  How I can do this by the linq or using the iterators and related calculations is a better way? Thanks.</p>
  <p>Current logic without linq:</p>
  <pre><code>IOrderedEnumerable&lt;Address&gt; addresses = addressesList
      .Where(value =&gt; value.Balance &gt; 0)
      .OrderByDescending(value =&gt; value.Balance);
  
  List&lt;SendTask&gt; selectedTasks = new List&lt;SendTask&gt;();
  int lastSelectedTaskIndex = 0;
  SendTask[] tasks = tasksToProcess.ToArray();
  int tasksCount = tasksToProcess.Count();
  
  foreach (Address address in addresses)
  {
      decimal balance = 0;
                  
      for(int i = lastSelectedTaskIndex; i &lt; tasksCount; i++)
      {
          SendTask task = tasks[i];
          balance += task.TransactionAmount;
  
          if (balance &lt; address.Balance)
          {
              selectedTasks.Add(task);
              lastSelectedTaskIndex++;
          }
          else 
          {
              break;
          }
      }
  }
  
  notSelectedTasks = tasksToProcess.Except(selectedTasks).Select(task =&gt; task.TaskId);
  </code></pre>