Ejemplo n.º 1
0
        public static DataMatrixBuilder Create <T>(IReadOnlyList <T> values, string[] columnHeaders,
                                                   params Func <T, object>[] valueSelectors)
        {
            Debug.Assert(columnHeaders.Length == valueSelectors.Length);

            if (columnHeaders.Length != valueSelectors.Length)
            {
                throw new DataMatrixException($"Column headers '{columnHeaders.Length}' not equal to valueSelectors '{valueSelectors.Length}'");
            }

            var result = new DataMatrixBuilder
            {
                _columnHeaders    = columnHeaders,
                _arrValues        = new object[values.Count, valueSelectors.Length],
                _dependentColumns = new List <DataMatrixColumn>()
            };

            // Fill table rows
            for (int rowIndex = 0; rowIndex < result._arrValues.GetLength(0); rowIndex++)
            {
                for (int colIndex = 0; colIndex < result._arrValues.GetLength(1); colIndex++)
                {
                    result._arrValues[rowIndex, colIndex] = valueSelectors[colIndex]
                                                            .Invoke(values[rowIndex]);
                }
            }

            return(result);
        }
Ejemplo n.º 2
0
        public static DataMatrixBuilder Create(DataTable table, params string[] columns)
        {
            if (columns == null || columns.Length < 1)
            {
                columns = (from DataColumn c in table.Columns select c.ColumnName).ToArray();
            }
            else
            {
                DataColumnCollection collection = table.Columns;
                foreach (var column in columns)
                {
                    if (!collection.Contains(column))
                    {
                        throw new ColumnNotFoundException(column);
                    }
                }
            }

            var result = new DataMatrixBuilder
            {
                _columnHeaders    = columns,
                _arrValues        = new object[table.Rows.Count, columns.Length],
                _dependentColumns = new List <DataMatrixColumn>()
            };

            var colIndexes = columns.Select(c => table.Columns[c].Ordinal).ToArray();

            // Fill table rows
            for (var rowIndex = 0; rowIndex < result._arrValues.GetLength(0); rowIndex++)
            {
                for (var colIndex = 0; colIndex < colIndexes.Length; colIndex++)
                {
                    result._arrValues[rowIndex, colIndex] = table.Rows[rowIndex][colIndexes[colIndex]];
                }
            }

            return(result);
        }