EPPlus.DataExtractor icon indicating copy to clipboard operation
EPPlus.DataExtractor copied to clipboard

Extend termination criteria

Open balintn22 opened this issue 4 years ago • 0 comments

Hi,

I really like the simple approach, but imho DataExtractor.GetData() termination is lacking: it can either terminate based on the row number or on the value of a single property. I propose to extend the termination test like this:

` ///

/// Obtains the entities for the columns previously configured. /// The indicates the initial row that will be read, /// the data extraction will only occur while the predicate returns true. /// It'll get executed receiving the row index as parameter before extracting the data of each row. /// /// The initial row to start the data extraction. /// The condition that is evaulated for each row. The last fetched record is /// passed as the argument to the predicate. The condition is evaluated before the row under test is /// returned. Row fetching stops when the test returns false. /// Returns an with the data of the columns. public IEnumerable<TRow> GetDataUntil(int fromRow, Func<int, TRow, bool> whileFunc) { if (whileFunc is null) throw new ArgumentNullException(nameof(whileFunc));

        int row = fromRow;
        while(true)
        {
            var dataInstance = new TRow();

            bool continueExecution = true;
            for (int index = 0; continueExecution && index < this.propertySetters.Count; index++)
                continueExecution = this.propertySetters[index].SetPropertyValue(dataInstance, row, this.worksheet.Cells);

            if (!continueExecution)
            {
                yield return dataInstance;
                break;
            }

            foreach (var collectionPropertySetter in this.collectionColumnSetters)
                collectionPropertySetter.SetPropertyValue(dataInstance, row, this.worksheet.Cells);

            foreach (var simpleCollectionColumnSetter in this.simpleCollectionColumnSetters)
                simpleCollectionColumnSetter.SetPropertyValue(dataInstance, row, this.worksheet.Cells);

            if(!whileFunc(row, dataInstance))
                break;

            yield return dataInstance;

            row++;
        }
    }

`

balintn22 avatar Feb 06 '21 14:02 balintn22