Let's say I have a class TmpClass, I use this class for returning data from Excel sheet using LinqToExcel ExcelQueryFactory. In this class I need to implement some logic to check data correctness. When I retrieve data from Excel sheet using ExcelQueryFactory it returns ExcelQueryable (which descends from IQueryalbe) There can be validation problems with multiple TmpClass instances - how can I process them in a collections style?
Class for Excel sheet data mapping
public class TmpClass
{
private string _name;
public string Name
{
set {
if (string.IsNullOrWhiteSpace(value))
{
throw new ArgumentException("Value can not be null/whitespace!");
}
_name = value;
}
}
}
retrieving data from Excel sheet with LinqToExcel ExcelQueryFactory
..initialize ExcelQueryFactory instance, get column names, set column mapping
//retrieve data
var dataFromExcel = excel.Worksheet<TmpClass>(...worksheet name).ToList();
When reading Excel sheet rows, for some of them TmpClass.Name setter function will throw ArgumentException - I need a way to process all the Excel sheet rows:
Can I get a collection of invalid items (with exception info perhaps)? Can I get a collection of valid items?
It may be hard to do even if you will iterate items manually.
var enumerator = excel.Worksheet<TmpClass>(...).GetEnumerator();
while (true)
{
try
{
if (!enumerator.MoveNext())
break;
// use enumerator.Current some way
}
catch
{
// you can't use enumerator.Current
}
}
As the exception occurs when calling MoveNext
, it's impossible to know whether there are more items in an enumerable or not. And it's not guaranteed that MoveNext
had moved a pointer on element actually.
Therefore, apparently, the easiest way is to create another class like the TmpClass
without any business-logic, just to store values, and to use this new class.