Consider this brief snippet:
var candidateWords = GetScrabblePossibilities(letters);
var possibleWords = new List<String>();
foreach (var word in candidateWords)
{
if (word.Length == pattern.Length)
{
bool goodMatch = true;
for (int i=0; i < pattern.Length && goodMatch; i++)
{
var c = pattern[i];
if (c!='_' && word[i]!=c)
goodMatch = false;
}
if (goodMatch)
possibleWords.Add(word);
}
}
Is there a way to express this cleanly using LINQ?
What is it?
A straightforward translation would be to overlay each candidate-word over the pattern-word using the
Zipoperator.If you really want to focus on the indices, you can use the
Rangeoperator:EDIT:
As David Neale points out, the
Zipoperator is not available before .NET 4.0. It’s trivial to implement it yourself, however.