Say I have the following code:
class SampleClass
{
public int Id {get; set;}
public int Name {get; set;}
}
List<SampleClass> myList = new List<SampleClass>();
//list is filled with objects
...
string nameToExtract = "test";
So my question is what List function can I use to extract from myList
only the objects that have a Name property that matches my nameToExtract
string.
I apologize in advance if this question is really simple/obvious.
Further to the other answers suggesting LINQ, another alternative in this case would be to use the FindAll
instance method:
List<SampleClass> results = myList.FindAll(x => x.Name == nameToExtract);
myList.Where(item=>item.Name == nameToExtract)
using System.Linq;
list.Where(x=> x.Name == nameToExtract);
Edit: misread question (now all matches)
Source: Stackoverflow.com