sooo, friday night, best time for practicing :) I am struggling with this code:
var collection = rows.Skip(skipHeader ? 1 : 0)
.Select(row =>
{
try
{
var tnew = new T();
columns.ForEach(col =>
{//for simplicity removed some code
var val = worksheet.Cells[row, col.Column];
if (col.Property.PropertyType == typeof(double))
{
col.Property.SetValue(tnew, val.GetValue<double>());
return;
}
col.Property.SetValue(tnew, val.GetValue<string>());
});
return tnew;
}
catch (Exception e)
{
Console.WriteLine($"Could not create object from excel: {e}");
//return default(T); dont do anything <<=== here i the problem
}
}).ToList();
The idea is this generic peace of code reads from excel a row and create new T
object. However, if could be possible that there is more data in the row/excel that needs to be (metadata that i don't need).
So basicly I need to check if all goes well. If it goes into the exception, then I know it doesn't fit the modal, so skip that row.
any thoughts?