-1

I have an application can compare two lsts of data the user inputs. Currently however, if two values are the same but one is upper case the application classes this as not a match.

Now I have used StringComparison.InvariantCultureIgnoreCase before but I am unsure of how to add this within a foreach loop.

My current code is as follows;

List<string> mylistA = new List<string>();
List<string> mylistB = new List<string>();

if (listATextBox.LineCount > 0) 
    for (int i = 1; i <= listATextBox.LineCount; i++) 
        mylistA.Add(listATextBox.GetLineText(i - 1).Replace(Environment.NewLine, "").Trim());

if (listBTextBox.LineCount > 0) 
    for (int i = 1; i <= listBTextBox.LineCount; i++) 
        mylistB.Add(listBTextBox.GetLineText(i - 1).Replace(Environment.NewLine, "").Trim());

foreach (string line in mylistA)
{             
    if (mylistB.Contains(line))
    {
            ResultsToDocument();
    }
}
mm8
  • 163,881
  • 10
  • 57
  • 88

4 Answers4

4

You can use Enumerable.Contains with this comparer as parameter instead of List.Contains:

foreach (string line in mylistA)
{
    if (mylistB.Contains(line, StringComparer.InvariantCultureIgnoreCase))
    {
        ResultsToDocument();
    }
}

A more efficient approach would be Intersect which also has this overload:

foreach(string line in mylistA.Intersect(mylistB, StringComparer.InvariantCultureIgnoreCase))
     ResultsToDocument(); // does this make sense, not passing the line?
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
  • yeah this does make sense, so you don't have to pass the line and the check is done at the 'foreach' and you don't need the `if` statement. –  Sep 29 '17 at 10:57
  • One thing you have to be careful with when using `intersect` is that if there are duplicates in `mylistA` it will not generate duplicates in the result. Like the loop in the question will. – Magnus Sep 29 '17 at 11:09
2

Try this:

foreach (string line in mylistA)
{
    if (mylistB.Any(b => b.Equals(line, StringComparison.OrdinalIgnoreCase)))
    {
        ResultsToDocument();
    }
}
mm8
  • 163,881
  • 10
  • 57
  • 88
2

For better performance and readability you can use Intersect

foreach (var element in mylistA.Intersect(mylistB, StringComparer.OrdinalIgnoreCase))
   ResultsToDocument();

If there are duplicates in mylistA that needs to be preserved. Using a hashset for the lookup would be the right way to go and would have the same performance as the intersect solution.

var set = new HashSet<string>(mylistB, StringComparer.OrdinalIgnoreCase)
foreach (string line in mylistA)
{             
    if (set.Contains(line))
    {
         ResultsToDocument();
    }
}
Magnus
  • 45,362
  • 8
  • 80
  • 118
-2

I'd go with:

    foreach (string lineA in mylistA)
    {     
       foreach (string lineB in mylistB)
       {                     
            if (lineB.ToUpper() == lineA.ToUpper())
            {
                  ResultsToDocument();
            }
       }
    }

but there are probably better solutions to achieve the same result