Anyone know how to (or if its possible) when using linq if one of the iterations fails to just ignore the fails and keep the successful. Example: when I’m importing a CSV:
string[] csvRows = File.ReadAllLines(fiList[fileCount].FullName);
var Rows = from row in csvRows.Skip(1)
let data = row.Split(',')
select new
{
Reference = data[0],
RequestDate = Convert.ToDateTime(data[1]),
};
and the csv looks like:
Reference, RequestDate ref1,20/06/10
00:00:00 ref2,55/06/10 00:00:00
Obviously, it’ll fail on the second one as it can’t convert to a datetime (“String was not recognized as a valid DateTime.”). Is there any way to force .net to ignore this and just return the first row?
Cheers in advance
Stu
You could test and filter out the invalid rows:
Personally I’d go for a
foreachloop in this case which would make the code more readable.An alternative to support readability is to factor out the validity testing (just an example):