<p>I was writing some <strong>ASP.NET</strong> control when I came to the scenario where I needed to remove items from a list, only when they matched a certain condition.</p>

<p>The <code>RemoveAll</code> method of the generic <code>List</code> class does a good job, but removes <em>all</em> items that match the condition, specified by the predicate.</p>

<p>What if I want to only remove a certain number of items specifying the condition? What do you think is the best way to do this?</p>

[{'answer_id': 16464, 'author': 'mercutio', 'author_id': 1951, 'author_profile': 'https://Stackoverflow.com/users/1951', 'pm_score': 1, 'selected': False, 'text': '<p>Unless the method provides a "limit" parameter (which it doesn\'t) your best option is to go with a simple loop that removes the items that match, breaking when your incremented "match counter" hits your limit.</p>\n\n<p>That\'s pretty much how the internal function works anyway, but in a more optimized way.</p>\n'}, {'answer_id': 16466, 'author': 'Serhat Ozgel', 'author_id': 31505, 'author_profile': 'https://Stackoverflow.com/users/31505', 'pm_score': 1, 'selected': False, 'text': '<p>In framework 3.5, RemoveAll method takes a predicate as a parameter. So you may use</p>\n\n<pre><code>list.RemoveAll(item =&gt; ShouldIRemoveThis(item));\n</code></pre>\n\n<p>where ShouldIRemoveThis is a method that returns a boolean indicating whether the item must be removed from the list.</p>\n'}, {'answer_id': 16468, 'author': 'Jon Galloway', 'author_id': 5, 'author_profile': 'https://Stackoverflow.com/users/5', 'pm_score': 0, 'selected': False, 'text': '<p>Can you use LINQ? If so, you can just use the .Take() method and specify how many records you want (maybe as total - N).</p>\n'}, {'answer_id': 16493, 'author': 'Adam Wright', 'author_id': 1200, 'author_profile': 'https://Stackoverflow.com/users/1200', 'pm_score': 0, 'selected': False, 'text': '<p>Anonymous delegates are useful here. A simple example to remove the first <em>limit</em> even numbers from a list.</p>\n\n<pre><code>List&lt;int&gt; myList = new List&lt;int&gt;;\nfor (int i = 0; i &lt; 20; i++) myList.add(i);\n\nint total = 0;\nint limit = 5;\nmyList.RemoveAll(delegate(int i) { if (i % 2 == 0 &amp;&amp; total &lt; limit) { total++; return true; } return false; });\n\nmyList.ForEach(i =&gt; Console.Write(i + " "));\n</code></pre>\n\n<p>Gives 1 3 5 7 9 10 11 12 13 14 15 16 17 18 19, as we want. Easy enough to wrap that up in a function, suitable for use as a lambda expression, taking the real test as a parameter.</p>\n'}, {'answer_id': 16496, 'author': 'Wilka', 'author_id': 1367, 'author_profile': 'https://Stackoverflow.com/users/1367', 'pm_score': 3, 'selected': False, 'text': '<p>@buyutec</p>\n\n<p>Instead of</p>\n\n<pre><code>list.RemoveAll(item =&gt; ShouldIRemoveThis(item));\n</code></pre>\n\n<p>you can use:</p>\n\n<pre><code>list.RemoveAll(ShouldIRemoveThis);\n</code></pre>\n\n<p>The lambda has the same signature as the method, so they are equivalent so you can just pass the method directly.</p>\n'}, {'answer_id': 16500, 'author': 'Serhat Ozgel', 'author_id': 31505, 'author_profile': 'https://Stackoverflow.com/users/31505', 'pm_score': 4, 'selected': True, 'text': '<p>If you want to specify both a limit for number of items to remove and a condition to select the items to remove, you can use this approach:</p>\n\n<pre><code>int limit = 30; // Suppose you want to remove 30 items at most\nlist.RemoveAll(item =&gt; ShouldIRemoveThis(item) &amp;&amp; limit-- &gt; 0);\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/16460', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1801/']