I constructed a variable by parsing a text file with some addresses.
FileInfo fi = new FileInfo(@"C:\temp\Addresses.txt")
var ZipCodesAndCountryCodes = File.ReadLines(fi.FullName)
.Select(l => new
{
ZipCode = l.Substring(1395, 5),
CountryCode = String.IsNullOrWhiteSpace(l.Substring(1405,30))
? "US"
: l.Substring(1405,30)
});
In this code, I'm replacing any blank value for country with "US". However I also want to normalize it to "US", if the country is "United States" or "United States of America" or "USA". How can I do that in LINQ? If it is any other country it should be included as it is.
Speed is a consideration too as the text files I'll be parsing will be 800MB or so. Thank you for any help.
UPDATE1: I'm getting this error when I tried Mark's and Aush's answers:
System.ObjectDisposedException: Cannot read from a closed TextReader.
at System.IO.__Error.ReaderClosed()
at System.IO.StreamReader.ReadLine()
at System.IO.File.<InternalReadLines>d__0.MoveNext()
at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
at System.Linq.Lookup`2.Create[TSource](IEnumerable`1 source, Func`2 keySelector, Func`2 elementSelector, IEqualityComparer`1 comparer)
at System.Linq.GroupedEnumerable`3.GetEnumerator()
at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
at AnthemMDTS.Program.Main(String[] args) in c:\Projects\CustomerA\CustomerATax\Program.cs:line 100
What is the TextReader in question here? I'm not closing anything nor there is any looping going on in the code.