You can do this using the extension method for IEnumerable<string> , for example:
public static class EnumerableExtensions { public static bool Contains( this IEnumerable<string> source, string value, StringComparison comparison ) { if (source == null) { return false;
then change
if (destlinestotal.Contains( sline ))
to
if (destlinestotal.Contains( sline, StringComparison.OrdinalIgnoreCase ))
However, if the sets are large and / or you are going to do it very often, then how you do it is very inefficient. Essentially, you perform the O (n 2 ) operation β for each line in the source, you compare it to potentially all the lines in the destination. It would be better to create a HashSet from the destination columns using a case insenstivie comparator, and then iterate over the source columns to see if each of them exists in the HashSet of the destination columns. This would be an O (n) algorithm. note that Contains on the HashSet will use the comparator that you provide in the constructor.
string[] sourcelinestotal = File.ReadAllLines("C:\\testdirectory\\" + "sourcecolumns.txt"); HashSet<string> destlinestotal = new HashSet<string>( File.ReadAllLines("C:\\testdirectory\\" + "destcolumns.txt"), StringComparer.OrdinalIgnoreCase ); foreach (string sline in sourcelinestotal) { if (!destlinestotal.Contains(sline)) { File.AppendAllText("C:\\testdirectory\\" + "missingcolumns.txt", sline); } }
In retrospect, I really prefer this solution by simply writing my own case insensitive contained for IEnumerable<string> unless you need a method for something else. There is actually less code (of your own) to support using the HashSet implementation.
source share