Пример #1
0
        /// <summary>
        /// If needed, and not in append mode - add skipped lines,
        /// and headings
        /// </summary>
        /// <param name="preambleLines">Pre-amble lines to add</param>
        public void WritePreamble(string[] preambleLines = null)
        {
            InitialiseWrite();
            if (AppendMode)
            {
                _writeInitialised = true;
                return;
            }

            if (Delimiter == null)
            {
                throw new ArgumentNullException("Delimiter");
            }

            if (preambleLines != null)
            {
                foreach (string line in preambleLines)
                {
                    _writeStream.WriteLine(line);
                }
            }

            if (FirstRowHeader)
            {
                var headerRow = RelevantMetaData.Select((a, i) => a.ColumnHeader ?? string.Concat("Column " + i))
                                .Aggregate((a, b) => string.Concat(a, Delimiter, b));

                _writeStream.WriteLine(headerRow);
            }

            _writeInitialised = true;
        }
Пример #2
0
        /// <summary>
        /// Write lines for the specified records to the output stream
        /// </summary>
        /// <param name="records">Records of the target type to serialize to text</param>
        public void WriteLines(IEnumerable <T> records)
        {
            if (Delimiter == null)
            {
                throw new ArgumentNullException("Delimiter");
            }

            if (_writeInitialised == false)
            {
                WritePreamble(PreambleLines);
            }

            foreach (T record in records)
            {
                var writeData = string.Join(Delimiter, RelevantMetaData.Select(a => a.GetValue(record)));
                _writeStream.WriteLine(writeData);
            }
        }