/// <summary> /// Asynchronously writes all rows in <paramref name="this"/> to <paramref name="csvWriter"/>. /// </summary> /// <remarks> /// </remarks> /// <param name="this"> /// The <see cref="DataTable"/>. /// </param> /// <param name="csvWriter"> /// The <see cref="CsvWriter"/>. /// </param> /// <param name="writeHeaderRecord"> /// If <see langword="true"/>, a header record will also be written, which will be comprised of the column names defined for <paramref name="this"/>. /// </param> /// <param name="maximumRows"> /// The maximum number of rows from <paramref name="this"/> that should be written to <paramref name="csvWriter"/>. /// </param> /// <param name="objectToStringConverter"> /// Provides a means of converting values in the <see cref="DataRow"/>s to <see cref="String"/>s. /// </param> /// <returns> /// The actual number of rows from <paramref name="this"/> written to <paramref name="csvWriter"/>. /// </returns> public async static Task <int> WriteCsvAsync(this DataTable @this, CsvWriter csvWriter, bool writeHeaderRecord, int?maximumRows, Func <object, string> objectToStringConverter) { @this.AssertNotNull("@this"); csvWriter.AssertNotNull("csvWriter"); objectToStringConverter.AssertNotNull("objectToStringConverter"); var num = 0; if (writeHeaderRecord) { var columnNames = new string[@this.Columns.Count]; for (var i = 0; i < columnNames.Length; ++i) { columnNames[i] = @this.Columns[i].ColumnName; } await csvWriter.WriteRecordAsync(columnNames).ConfigureAwait(false); } var maximum = maximumRows.GetValueOrDefault(int.MaxValue); var buffer = new DataRecord[16]; var bufferOffset = 0; foreach (DataRow row in @this.Rows) { var record = new DataRecord(); for (var i = 0; i < row.ItemArray.Length; ++i) { record.Add(objectToStringConverter(row.ItemArray[i])); } buffer[bufferOffset++] = record; if (bufferOffset == buffer.Length) { // buffer full await csvWriter.WriteRecordsAsync(buffer, 0, buffer.Length).ConfigureAwait(false); bufferOffset = 0; } if (++num == maximum) { break; } } // write any outstanding data in buffer await csvWriter.WriteRecordsAsync(buffer, 0, bufferOffset).ConfigureAwait(false); return(num); }
/// <summary> /// Asynchronously copies all remaining records in <paramref name="this"/> to <paramref name="destination"/>. /// </summary> /// <param name="this"> /// The data source. /// </param> /// <param name="destination"> /// The data destination. /// </param> /// <returns> /// The number of records written to <paramref name="destination"/>. /// </returns> public async static Task<int> CopyToAsync(this CsvReader @this, CsvWriter destination) { @this.AssertNotNull("@this"); destination.AssertNotNull("destination"); var num = 0; var buffer = new DataRecord[16]; var read = 0; while ((read = await @this.ReadDataRecordsAsync(buffer, 0, buffer.Length).ConfigureAwait(false)) != 0) { await destination.WriteRecordsAsync(buffer, 0, read).ConfigureAwait(false); num += read; } return num; }
/// <summary> /// Copies all remaining records in <paramref name="this"/> to <paramref name="destination"/>. /// </summary> /// <param name="this"> /// The data source. /// </param> /// <param name="destination"> /// The data destination. /// </param> /// <returns> /// The number of records written to <paramref name="destination"/>. /// </returns> public static int CopyTo(this CsvReader @this, CsvWriter destination) { @this.AssertNotNull("@this"); destination.AssertNotNull("destination"); var num = 0; var buffer = new DataRecord[16]; var read = 0; while ((read = @this.ReadDataRecords(buffer, 0, buffer.Length)) != 0) { destination.WriteRecords(buffer, 0, read); num += read; } return num; }
/// <summary> /// Copies all remaining records in <paramref name="this"/> to <paramref name="destination"/>. /// </summary> /// <param name="this"> /// The data source. /// </param> /// <param name="destination"> /// The data destination. /// </param> /// <returns> /// The number of records written to <paramref name="destination"/>. /// </returns> public static int CopyTo(this CsvReader @this, CsvWriter destination) { @this.AssertNotNull("@this"); destination.AssertNotNull("destination"); var num = 0; var buffer = new DataRecord[16]; var read = 0; while ((read = @this.ReadDataRecords(buffer, 0, buffer.Length)) != 0) { destination.WriteRecords(buffer, 0, read); num += read; } return(num); }
/// <summary> /// Asynchronously copies all remaining records in <paramref name="this"/> to <paramref name="destination"/>. /// </summary> /// <param name="this"> /// The data source. /// </param> /// <param name="destination"> /// The data destination. /// </param> /// <returns> /// The number of records written to <paramref name="destination"/>. /// </returns> public async static Task <int> CopyToAsync(this CsvReader @this, CsvWriter destination) { @this.AssertNotNull("@this"); destination.AssertNotNull("destination"); var num = 0; var buffer = new DataRecord[16]; var read = 0; while ((read = await @this.ReadDataRecordsAsync(buffer, 0, buffer.Length).ConfigureAwait(false)) != 0) { await destination.WriteRecordsAsync(buffer, 0, read).ConfigureAwait(false); num += read; } return(num); }
/// <summary> /// Asynchronously writes the items in <paramref name="this"/> to <paramref name="csvWriter"/>. /// </summary> /// <remarks> /// <para> /// This overload provides maximum flexibility in how items are written CSV. /// </para> /// </remarks> /// <typeparam name="T"> /// The type of the items to be written to <paramref name="csvWriter"/>. /// </typeparam> /// <param name="this"> /// The items to write. /// </param> /// <param name="csvWriter"> /// The <see cref="CsvWriter"/>. /// </param> /// <param name="header"> /// If non-<see langword="null"/>, this will be written to <paramref name="csvWriter"/> before any data records are written. /// </param> /// <param name="objectToRecordConverter"> /// Converts an item in <paramref name="this"/> to a CSV record. /// </param> /// <returns> /// The number of items written. /// </returns> public async static Task <int> WriteCsvAsync <T>(this IEnumerable <T> @this, CsvWriter csvWriter, IEnumerable <string> header, Func <T, IEnumerable <string> > objectToRecordConverter) { @this.AssertNotNull("@this"); csvWriter.AssertNotNull("csvWriter"); objectToRecordConverter.AssertNotNull("objectToRecordConverter"); HeaderRecord headerRecord = null; if (header != null) { headerRecord = new HeaderRecord(header); await csvWriter.WriteRecordAsync(headerRecord).ConfigureAwait(false); } var num = 0; var buffer = new DataRecord[16]; var bufferOffset = 0; foreach (var item in @this) { var record = new DataRecord(headerRecord, objectToRecordConverter(item)); buffer[bufferOffset++] = record; if (bufferOffset == buffer.Length) { // buffer full await csvWriter.WriteRecordsAsync(buffer, 0, buffer.Length).ConfigureAwait(false); bufferOffset = 0; } ++num; } // write any outstanding data in buffer await csvWriter.WriteRecordsAsync(buffer, 0, bufferOffset).ConfigureAwait(false); return(num); }
/// <summary> /// Asynchronously writes all rows in <paramref name="this"/> to <paramref name="csvWriter"/>. /// </summary> /// <remarks> /// </remarks> /// <param name="this"> /// The <see cref="DataTable"/>. /// </param> /// <param name="csvWriter"> /// The <see cref="CsvWriter"/>. /// </param> /// <param name="writeHeaderRecord"> /// If <see langword="true"/>, a header record will also be written, which will be comprised of the column names defined for <paramref name="this"/>. /// </param> /// <param name="maximumRows"> /// The maximum number of rows from <paramref name="this"/> that should be written to <paramref name="csvWriter"/>. /// </param> /// <param name="objectToStringConverter"> /// Provides a means of converting values in the <see cref="DataRow"/>s to <see cref="String"/>s. /// </param> /// <returns> /// The actual number of rows from <paramref name="this"/> written to <paramref name="csvWriter"/>. /// </returns> public async static Task<int> WriteCsvAsync(this DataTable @this, CsvWriter csvWriter, bool writeHeaderRecord, int? maximumRows, Func<object, string> objectToStringConverter) { @this.AssertNotNull("@this"); csvWriter.AssertNotNull("csvWriter"); objectToStringConverter.AssertNotNull("objectToStringConverter"); var num = 0; if (writeHeaderRecord) { var columnNames = new string[@this.Columns.Count]; for (var i = 0; i < columnNames.Length; ++i) { columnNames[i] = @this.Columns[i].ColumnName; } await csvWriter.WriteRecordAsync(columnNames).ConfigureAwait(false); } var maximum = maximumRows.GetValueOrDefault(int.MaxValue); var buffer = new DataRecord[16]; var bufferOffset = 0; foreach (DataRow row in @this.Rows) { var record = new DataRecord(); for (var i = 0; i < row.ItemArray.Length; ++i) { record.Add(objectToStringConverter(row.ItemArray[i])); } buffer[bufferOffset++] = record; if (bufferOffset == buffer.Length) { // buffer full await csvWriter.WriteRecordsAsync(buffer, 0, buffer.Length).ConfigureAwait(false); bufferOffset = 0; } if (++num == maximum) { break; } } // write any outstanding data in buffer await csvWriter.WriteRecordsAsync(buffer, 0, bufferOffset).ConfigureAwait(false); return num; }