Monday, 15 July 2013

Using .Except without case sensitivity C# -



Using .Except without case sensitivity C# -

i working on project reads in 2 csv files:

var myfullcsv = readfile(myfullcsvfilepath); var mastercsv = readfile(csvfilepath);

and creates new var containing lines exist in myfullcsv not master csv. code great because of simplicity:

var extrafilescsv = myfullcsv.except(mastercsv);

the csv files read in contain info this:

c01.jpg,95182,24f77a1e,\folder1\foldera\, c02.jpg,131088,c17b1f13,\folder1\foldera\, c03.jpg,129485,ddc964ec,\folder1\foldera\, c04.jpg,100999,930ee633,\folder1\foldera\, c05.jpg,101638,b89f1f28,\folder1\foldera\,

however, have found situation case of characters in each file not match. illustration (jpg in caps):

c01.jpg,95182,24f77a1e,\folder1\foldera\,

if info not included in extrafilescsv need be. can tell me how can create code insensitive case of text?

edit: sorry, forgot readfile not standard command. here code:

public static ienumerable<string> readfile(string path) { string line; using (var reader = file.opentext(path)) while ((line = reader.readline()) != null) yield homecoming line; }

i'm assuming you've read in both csv files , have collection of strings representing each file.

you can specify specific equalitycomparer in phone call except(), instructs on type of comparing between 2 collections of objects.

you can create own comparer or, assuming both collections of strings, seek specifying existing 1 ignores case:

var extrafilescsv = myfullcsv.except(mastercsv, stringcomparer.currentcultureignorecase);

by default, if don't specify comparer, uses equalitycomparer<telement>.default, differs based on class type you're comparing.

for strings, first straight-up a==b comparing default, case-sensitive. (the exact implementation on the string class little more complicated, it's unnecessary post here.)

c#

No comments:

Post a Comment