سؤال

public class abcd
{
    public string sampleA;
    public string sampleB;

    public abcd(string _sampleA, string _sampleB)
    {
        sampleA = _sampleA;
        sampleB = _sampleB;
    }
}

string filter = "Filter";
List<abcd> listA = new List<abcd>();

I am trying to figure out how to write a foreach statement to retrieve all the class instances in my listA that have sampleA contain the string filter.

Basically foreach abcd sampleA in listA that contain filter do this...

Thanks in advance for any help

هل كانت مفيدة؟

المحلول

foreach(abcd obj in listA.Where(a => a.sampleA.Contains("Filter")))
{
    // ...
}

if you want to compare case-insensitive:

foreach (abcd obj in listA
    .Where(a => a.sampleA.IndexOf("Filter", StringComparison.CurrentCultureIgnoreCase) >= 0))
{
    // ...
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top