How to use linq and string.EndsWith (<1 of N elements in an array>)

I have an expression as indicated above in the header. Is it possible to use linq to iterate over an array and evaluate whether it is valid or not. example string[] a = {"es","ag"}

if (string.EndsWith(<1 of N items in a>)==true){//do something}
+3
source share
2 answers
if (a.Any(yourString.EndsWith))
{
    //your string ends with one of those endings.
}
+5
source

The following code checks to see if myStringany of the lines in the array end a:

if(a.Any(s => myString.EndsWith(s))
{
     // do something
}
+2
source

All Articles