Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I perform a FindAll() on an IList<T>? (eg. SortedList.Values)
    text
    copied!<p>I'm working on a problem in C# 2.0/.NET 2.0 where I have a Sortedlist and want to search all the "values" (not the "keys") of this SortedList for a certain substring and count up how many occurrences there are.</p> <p>This is what I'm trying to do:</p> <pre><code>{ Sortedlist&lt;string,string&gt; mySortedList; // some code that instantiates mySortedList and populates it with data List&lt;string&gt; myValues = mySortedList.Values; // &lt;== does not work int namesFound = myValues.FindAll(ByName(someName)).Count; } </code></pre> <p>Naturally, this doesn't work because mySortedList.Values returns an IList, while "myValues" is a List. I tried "casting" the IList so that it would be accepted by myValues, but it doesn't seem to work.</p> <p>Of course, I can loop over mySortedList.Values in a "foreach" loop, but I don't really want to do this.</p> <p>Anyone have any suggestions?</p> <p>EDIT-1: Ok, well it looks like there isn't a native way to do this easily. I had assumed that I was just missing something, but apparently I'm not. So I guess I'm just going to do a "foreach" over the IList. </p> <p>Thanks for the feedback everyone! I voted everyone up 1 because I thought all the feedback was good. Thanks again! :-)</p> <p>EDIT-2: Looks like CMS has the answer I was looking for. The only caveat with this (as Qwertie pointed out) is that there is a potential performance penalty since it involves copying all the values to another List and then searching that list start-to-finish. So for short lists, this answer is effective. Longer lists? well that's up to you to decide...</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload