-1

Suppose I've an Arraylist(arr1) like the below

 "String1 is present"
 "String2 is present"
 "String3 is present"

i wanted to see if 'String2' is present in this arraylist. i've done something like the below:

        var containsstringmatch = arr1.OfType<string>().Any(arg=>arg.Contains("String2"));

        if (containsstringmatch==true)
        {
            IEnumerable v1 = arr1.OfType<string>().Where(arg=>arg.Contains("String2"));

            foreach (string s in v1)
            {
                st1 = s;
            }

            Console.WriteLine(st1);
        }

which gives me the below output which is good:

"String2 is present"

I wanted to see if this can be achieved without me using the foreach loop. Can someone please provide suggestions as to how to do it.

Thanks

user3807741
  • 95
  • 4
  • 11
  • 1
    Hey, not sure what the intent of the code is. Clearly you're doing more than just checking if the substring is present in any of the strings. Are you supposed to output every value that has the substring in the array or just the last one like your code does? – IOrlandoni Dec 10 '18 at 16:17
  • Related or duplicated https://stackoverflow.com/questions/200574/linq-equivalent-of-foreach-for-ienumerablet BTW, you should put the Console.Writeline of tour original code inside the foreach – Cleptus Dec 10 '18 at 16:37

3 Answers3

1

If you want only to print the first string that contains the search, you can use FirstOrDefault():

var foundString =  arr1.OfType<string>().FirstOrDefault(arg => arg.Contains("String2"));
Console.WriteLine(string.IsNullOrEmpty(foundString) ? "Not found" : foundString);

Also, as Aomine wrote in his answer - ArrayLists where good when we worked with .Net 1.1. Since .Net 2.0 introduced generics, ArrayLists should be avoided.

As Rufus L wrote in his comment, your current code gets the last string containing the search string, not the first. If you want the last and not the first, you can simply use LastOrDefault instead of FirstOrDefault.

Zohar Peled
  • 79,642
  • 10
  • 69
  • 121
  • the arraylist which i have will always have only 1 instance of a string (the above which i posted is just an example. The actually arraylist will have strings each starting with specific values which i cannot share here. My goal is to actually extract a specific string from the arraylist based on matching criteria and then do some validations on that later). – user3807741 Dec 10 '18 at 16:32
  • Well, in that case, `FirstOrDefault` or `LastOrDefault` are equivalent. – Zohar Peled Dec 10 '18 at 16:33
0

I'd avoid using ArrayList in this day and age in .NET, instead, favor the List<T> (if possible).

As for:

I wanted to see if this can be achieved without me using the foreach loop.

if by this you mean that you want to avoid the foreach construct and perform everything inline:

arr1.OfType<string>()
    .Where(arg => arg.Contains("String2"))
    .ToList()
    .ForEach(s => Console.WriteLine(s));

or if you just want to find the last element satisfying the said criteria:

var result = arr1.OfType<string>().LastOrDefault(arg => arg.Contains("String2"));
Ousmane D.
  • 54,915
  • 8
  • 91
  • 126
  • 1
    This would iterated twice right? Plus, OP wanted to avoid the `foreach` loop, I am not sure using the `ForEach` extension method is much better – maccettura Dec 10 '18 at 16:23
  • @maccettura when OP said "I wanted to see if this can be achieved without me using the foreach loop" I assumed they wanted to avoid the `foreach` construct and do everything inline... but maybe I am wrong? – Ousmane D. Dec 10 '18 at 16:24
-1

There is no way to do this without a foreach or for loop. But you can create an extension method that will move the code out of your method.

public static class ConsoleExtensions
{
    public static void WriteToConsole(this IEnumerable<string> list)
    {
        foreach (string item in list)
            Console.WriteLine(item);
    }
}

usage:

arr1.OfType<string>().Where(arg=>arg.Contains("String2")).WriteToConsole();
Steve Harris
  • 5,014
  • 1
  • 10
  • 25