MiniExcel icon indicating copy to clipboard operation
MiniExcel copied to clipboard

IEnumerable traversed twice

Open MadsSvejstrup opened this issue 3 years ago • 0 comments

In an attempt to use DynamicColumns with IDataReader i converted the reader into an IEnumerable<Dictionary<string, object>> using the following method:

private IEnumerable<Dictionary<string, object>> AsDict(IDataReader dataReader)
{
    while (dataReader.Read())
    {
        yield return Enumerable.Range(0, dataReader.FieldCount)
                .ToDictionary(
                    dataReader.GetName,
                    i => dataReader[i])
            ;
    }
}

However i discovered that this approach did not work because when passing an IEnumerable to MiniExcel.SaveAs() it is iterated twice. In the second iteration the IEnumerable is empty because is data from has dataReader has already been read.

I would be nice if MiniExcel supported proper deferred execution by only traversing the IEnumerable once.

MadsSvejstrup avatar Aug 25 '22 13:08 MadsSvejstrup