title: LINQ statement where result count is used in expression's condition
body: <p>O' LINQ-fu masters, please help.</p>
  
  <p>I have a requirement where I have to add items into a List(Of T) (let's call it Target) from an IEnumerable(Of T) (let's call it Source) using Target.AddRange() in VB.NET.</p>
  
  <pre><code>Target.AddRange(Source.TakeWhie(Function(X, Index) ?))
  </code></pre>
  
  <p>The ? part is a tricky condition that is something like: <em>As long as the as yet unenumerated count is not equal to what is needed to fill the list to the minimum required then randomly decide if the current item should be taken, otherwise take the item.</em>
  Somethig like...</p>
  
  <pre><code>Source.Count() - Index = _minimum_required - _curr_count_of_items_taken _
  OrElse GetRandomNumberBetween1And100() &lt;= _probability_this_item_is_taken
  ' _minimum_required and _probability_this_item_is_taken are constants
  </code></pre>
  
  <p>The confounding part is that _curr_count_of_items_taken needs to be incremented each time the TakeWhile statement is satisfied. How would I go about doing that?</p>
  
  <p>I'm also open to a solution that uses any other LINQ methods (Aggregate, Where, etc.) instead of TakeWhile.</p>
  
  <p>If all else fails then I will go back to using a good old for-loop =)</p>
  
  <p>But hoping there is a LINQ solution. Thanks in advance for any suggestions.</p>
  
  <p>EDIT: Good old for-loop version as requested:</p>
  
  <pre><code>Dim _source_total As Integer = Source.Count()
  For _index As Integer = 0 To _source_total - 1
      If _source_total - _index = MinimumRows - Target.Count _
      OrElse NumberGenerator.GetRandomNumberBetween1And100 &lt;= _possibility_item_is_taken Then
          Target.Add(Source(_index))
      End If
  Next
  </code></pre>
  
  <p>EDITDIT:
  David's no-side-effects answer comes closes to what I need while staying readable. Maybe he's the only one who could understand my poorly communicated pseudo-code =). The OrderBy(GetRandomNumber) is brilliant in hindsight. I just need to change the Take(3) part to Take(MinimumRequiredPlusAnOptionalRandomAmountExtra) and drop the OrderBy and Select at the end. Thanks to the rest for suggestions.</p>