Skip to content

40 Data transformations

René Pacios edited this page Jun 11, 2018 · 1 revision

Optionally, we can make transformations over data before populate in memory object. We can indicate an Expression (per field) for transform any data from Excel cell to adapt to target type

For example, we have column selected in Excel document with a ‘x’ mark and we want transform this data in Boolean value.

            //Example with trasnformations
            Expression<Func<string, DateTime>> fConvertData = _ => DateTime.Now.AddDays(10);
            Expression<Func<string, bool>> fSelectToBool = c => c == "x";

            var ls = wb.ReadTable<Models.TableWithHeadersAndBoleanConversion>(1,
                new ReadOptions()
                {
                    TitlesInFirstRow = true,
                    Converters = new Dictionary<string, LambdaExpression>()
                    {
                        {nameof(SimpleTable.ADate),fConvertData },
                        {nameof(SimpleTable.Selected),fSelectToBool },
                    }
                });

As show, we can transform any value from source.

  • Expression are defined in ReadOptions.Converters dictionary.
  • Dictionary entry key should be object field name, obviusly we can use nameof for that
Clone this wiki locally