Method for checking the list of arrays containing a specific string

I have an ArrayList that imports records from a database. Is there a way to check if the List schname array that I want to map to another list, which is an api, contains?

List<PrimaryClass> primaryList = new List<PrimaryClass>(e.Result); PrimaryClass sc = new PrimaryClass(); foreach (string item in str) { for (int a = 0; a <= e.Result.Count - 1; a++) { string schname = e.Result.ElementAt(a).PrimarySchool; string tophonour = e.Result.ElementAt(a).TopHonour; string cca = e.Result.ElementAt(a).Cca; string topstudent = e.Result.ElementAt(a).TopStudent; string topaggregate = e.Result.ElementAt(a).TopAggregate; string topimage = e.Result.ElementAt(a).TopImage; if (item.Contains(schname)) { } } } 

Here is what I have come up with so far, kindly correct any mistakes that I could make. Thanks.

+6
source share
4 answers

try it

 foreach( string row in arrayList){ if(row.contains(searchString)){ //put your code here. } } 
+5
source

What about ArrayList.Contains ?

+7
source

Ok, now you have shown that this is actually a List<T> , with LINQ should be easy:

 if (primaryList.Any(x => item.Contains(x.PrimarySchool)) 

Note that you really need to use foreach instead of a for loop to iterate through the list, unless you definitely need an index ... and if you are dealing with a list, using an indexer is easier than calling ElementAt .

+4
source
 // check all types var containsAnyMatch = arrayList.Cast<object>().Any(arg => arg.ToString() == searchText); // check strings only var containsStringMatch = arrayList.OfType<string>().Any(arg => arg == searchText); 
+2
source

Source: https://habr.com/ru/post/888505/


All Articles