It seems that IgnoreUnknownColumns property does the job,
Here the code I use:
/// <summary>
/// The input file without header.
/// </summary>
private readonly CsvFileDescription inputFileWithoutHeader = new CsvFileDescription
{
SeparatorChar = ',',
FirstLineHasColumnNames = false,
EnforceCsvColumnAttribute = true,
IgnoreUnknownColumns = true
};
/// <summary>
/// The input file with headers.
/// </summary>
private readonly CsvFileDescription inputFileWithHeaders = new CsvFileDescription
{
SeparatorChar = ',',
FirstLineHasColumnNames = true,
EnforceCsvColumnAttribute = false,
IgnoreUnknownColumns = true
};
/// <summary>
/// The list items.
/// </summary>
/// <returns>
/// The <see>
/// <cref>IEnumerable</cref>
/// </see>
/// .
/// </returns>
public IEnumerable<ListItem> ListItems()
{
return
Directory.EnumerateFileSystemEntries(this.path, "ListItem*.csv")
.SelectMany(chkLstFile => this.csvContext.Read<ListItem>(chkLstFile, this.inputFileWithoutHeader)).Distinct();
}
Then I retrieve my data from my repository:
var myItems = myClassInstance.ListItems().CatchExceptions(ex => Debug.WriteLine(ex.Message));
For more control I have an extension method to handle errors inspired from:
Wrap an IEnumerable and catch exceptions
public static IEnumerable<T> CatchExceptions<T>(this IEnumerable<T> src, Action<Exception> action = null)
{
using (var enumerator = src.GetEnumerator())
{
var next = true;
while (next)
{
try
{
next = enumerator.MoveNext();
}
catch (AggregatedException ex)
{
lock (ex)
{
foreach (var e in ex.m_InnerExceptionsList)
{
if (action != null)
{
action(e);
}
File.AppendAllText(LogFilePath, string.Format("{0}: {1}\r\n", DateTime.Now.ToShortTimeString(), e.Message)); //todo ILogger
}
}
File.AppendAllText(LogFilePath, "-\r\n");
continue;
}
catch (Exception ex)
{
if (action != null)
{
action(ex);
}
lock (ex)
{
File.AppendAllText(LogFilePath, string.Format("{0}: {1}\r\n", DateTime.Now.ToShortTimeString(), ex.Message)); //todo ILogger
}
continue;
}
if (next)
{
yield return enumerator.Current;
}
}
}
}