4

So, I check if a string contains a certain word from an array with the following statement:

if(stringArray.Any(s => stringToCheck.Contains(s)))

Simple. Now a match is found and the statement is true. But I want to know also which item in the array matched with the string. The placeholder "s" in the LINQ statement above is not available in the following clause.

Hope somebody has an idea. I could just loop through the array, yes, but LINQ looks way nicer to me. =)

Neurodefekt
  • 899
  • 2
  • 10
  • 18

4 Answers4

6
var match = stringArray.FirstOrDefault(s => stringToCheck.Contains(s));
if(match != null) {
    // match was found
}
Christian Hayter
  • 30,581
  • 6
  • 72
  • 99
  • 'var vMediaLvlResult = oSDP.GetMediaLevels().FirstOrDefault(s => vRequest.URL.ToString().Contains(s));' 'if (vMediaLvlResult != null)' '{' ' //true' '}' Worked. Thank you @Christian-Hayter, I wasn't aware of 'FirstOrDefault'. – Neurodefekt Dec 03 '12 at 08:14
  • @Tigran I just needed the string. ;) – Neurodefekt Dec 03 '12 at 08:28
3

If you want all strings in the input list that match the condition, you need to use Where:

var matchingStrings = stringArray.Where(s => stringToCheck.Contains(s))

If you just want the first string, you can use FirstOrDefault:

var firstMatchingString = stringArray
    .FirstOrDefault(s => stringToCheck.Contains(s))

For an overview of all available methods, check http://msdn.microsoft.com/en-us/library/bb341635.aspx

For an introduction to Linq to Objects, check out the 101 samples here: http://code.msdn.microsoft.com/101-LINQ-Samples-3fb9811b

jeroenh
  • 26,362
  • 10
  • 73
  • 104
  • @Tigran it wasn't clear from the question that the indexes are needed; your answer indeed provides that information. – jeroenh Dec 03 '12 at 08:23
1

Another simple solution (just one line of code) is just make use of Select overload:

With select we can access to the value and index contemporary:

//Example:
string[] arr = new[] {"a", "bb", "ccc"}; 
var result = arr.Select((v,i)=>new {value=v, index=i}).Where(x=>x.value.Contains("c"));

this produces result like:

{"ccc", 2}
Tigran
  • 61,654
  • 8
  • 86
  • 123
1

Another (more efficient) approach is Enumerable.Join:

IEnumerable<string> matches = from str1 in stringArray
                              join str2 in stringToCheck on str1 equals str2
                              select str1;
if(matches.Any())
{
    string allMatches = string.Join(", ", matches);
    Console.Write(allMatches);
}

Why is LINQ JOIN so much faster than linking with WHERE?

or maybe even more efficient using Enumerable.Intersect:

IEnumerable<string> matches = stringArray.Intersect(stringToCheck);
foreach(string common in matches)
{
    // ...
}
Community
  • 1
  • 1
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939