コード例 #1
0
ファイル: QueryBase.cs プロジェクト: craigbridges/Reportr
        /// <summary>
        /// Converts a collection of data into a list of query rows
        /// </summary>
        /// <typeparam name="T">The data type</typeparam>
        /// <param name="data">The data to convert</param>
        /// <returns>A list of query rows</returns>
        protected virtual List <QueryRow> ConvertToRows <T>
        (
            IEnumerable <T> data
        )
        {
            var localeConfiguration = this.DataSource.LocaleConfiguration;
            var rows       = new List <QueryRow>();
            var entityType = typeof(T);

            foreach (var item in data)
            {
                var cells = new List <QueryCell>();

                foreach (var info in this.Columns)
                {
                    var property = entityType.GetProperty
                                   (
                        info.Column.Name
                                   );

                    var propertyValue = property?.GetValue
                                        (
                        item
                                        );

                    var transformer = CulturalTransformerFactory.GetInstance
                                      (
                        propertyValue
                                      );

                    propertyValue = transformer.Transform
                                    (
                        propertyValue,
                        localeConfiguration
                                    );

                    cells.Add
                    (
                        new QueryCell
                        (
                            info.Column,
                            propertyValue
                        )
                    );
                }

                rows.Add
                (
                    new QueryRow
                    (
                        cells.ToArray()
                    )
                );
            }

            return(rows);
        }
コード例 #2
0
        /// <summary>
        /// Converts the results of a data reader to a collection of query rows
        /// </summary>
        /// <param name="reader">The data reader</param>
        /// <param name="queryColumns">The query columns</param>
        /// <returns>A collection of query rows</returns>
        public static IEnumerable <QueryRow> ToQueryRows
        (
            this SqlDataReader reader,
            ILocaleConfiguration localeConfiguration,
            params QueryColumnInfo[] queryColumns
        )
        {
            Validate.IsNotNull(localeConfiguration);

            var rows = new List <QueryRow>();

            if (reader.HasRows)
            {
                var columnSchemas = queryColumns.Select
                                    (
                    info => info.Column
                                    );

                while (reader.Read())
                {
                    var cells = new List <QueryCell>();

                    for (var i = 0; i < reader.FieldCount; i++)
                    {
                        var fieldName  = reader.GetName(i);
                        var fieldValue = reader.GetValue(i);

                        var transformer = CulturalTransformerFactory.GetInstance
                                          (
                            fieldValue
                                          );

                        fieldValue = transformer.Transform
                                     (
                            fieldValue,
                            localeConfiguration
                                     );

                        var columnSchema = columnSchemas.FirstOrDefault
                                           (
                            c => c.Name.Equals(fieldName, StringComparison.OrdinalIgnoreCase)
                                           );

                        if (columnSchema == null)
                        {
                            throw new InvalidOperationException
                                  (
                                      $"The field name '{fieldName}' was not expected."
                                  );
                        }

                        cells.Add
                        (
                            new QueryCell
                            (
                                columnSchema,
                                fieldValue
                            )
                        );
                    }
                }
            }

            return(rows);
        }