public static class CsvEnumerable
Namespace
CsvHelper.Linq
Assembly
CsvHelper.Linq.dll
Builds
net10.0 netstandard2.1 netstandard2.0 net47

Remarks

Writing records of types without a generated writer uses reflection on their public members, which DynamicallyAccessedMembersAttribute keeps for trimming and native AOT.

Methods

public static long AppendCsv<T>(this IEnumerable<T> records, string path, CsvWriterOptions? options = null)

Adds records to the end of a CSV file, creating it (with a header) when it does not exist or is empty. The records are written with the file's separator and line break, after a line break when the file does not end with one, and must be written with the header the file has.

Type parameters

T
The type of the records.

Parameters

recordsIEnumerable<T>
The records, enumerated once.
pathstring
The path of the file.
optionsCsvWriterOptions?
The options of the writer, or null for those of T with the file's separator and line break.

Returns

long
The number of records added.

Exceptions

ArgumentNullException
records or path is null.
InvalidOperationException
The file's header is not the one that the records are written with, or T has nothing to write.

View source

public static ValueTask<long> AppendCsvAsync<T>(this IAsyncEnumerable<T> records, string path, CsvWriterOptions? options = null, CancellationToken cancellationToken = default)

Adds an asynchronous sequence of records to the end of a CSV file asynchronously; see AppendCsv.

Type parameters

T
The type of the records.

Parameters

recordsIAsyncEnumerable<T>
The records, enumerated once.
pathstring
The path of the file.
optionsCsvWriterOptions?
The options of the writer, or null for those of T with the file's separator and line break.
cancellationTokenCancellationToken
Cancels reading the records and writing the file.

Returns

ValueTask<long>
The number of records added.

Exceptions

ArgumentNullException
records or path is null.
InvalidOperationException
The file's header is not the one that the records are written with, or T has nothing to write.

View source

public static ValueTask<long> AppendCsvAsync<T>(this IEnumerable<T> records, string path, CsvWriterOptions? options = null, CancellationToken cancellationToken = default)

Adds records to the end of a CSV file asynchronously; see AppendCsv.

Type parameters

T
The type of the records.

Parameters

recordsIEnumerable<T>
The records, enumerated once.
pathstring
The path of the file.
optionsCsvWriterOptions?
The options of the writer, or null for those of T with the file's separator and line break.
cancellationTokenCancellationToken
Cancels reading the records and writing the file.

Returns

ValueTask<long>
The number of records added.

Exceptions

ArgumentNullException
records or path is null.
InvalidOperationException
The file's header is not the one that the records are written with, or T has nothing to write.

View source

public static long WriteCsv<T>(this IEnumerable<T> records, CsvWriter writer)

Writes records to a CSV writer, with a header first when nothing has been written yet and its options have one. The writer stays open, and is not flushed.

Type parameters

T
The type of the records.

Parameters

recordsIEnumerable<T>
The records, enumerated once.
writerCsvWriter
The writer.

Returns

long
The number of records written, not counting the header.

Exceptions

ArgumentNullException
records or writer is null.
InvalidOperationException
T has nothing to write, or its records cannot be written.

View source

public static long WriteCsv<T>(this IEnumerable<T> records, string path, CsvWriterOptions? options = null)

Writes records to a new CSV file, with a header (unless the options say otherwise), replacing any file at path.

Type parameters

T
The type of the records.

Parameters

recordsIEnumerable<T>
The records, enumerated once.
pathstring
The path of the file.
optionsCsvWriterOptions?
The options of the writer, or null for those of T (CsvWriterOptions.For).

Returns

long
The number of records written, not counting the header.

Exceptions

ArgumentNullException
records or path is null.
InvalidOperationException
T has nothing to write, or its records cannot be written.

View source

public static long WriteCsv<T>(this IEnumerable<T> records, Stream stream, CsvWriterOptions? options = null, bool leaveOpen = false)

Writes records to a stream, as UTF-8, with a header (unless the options say otherwise).

Type parameters

T
The type of the records.

Parameters

recordsIEnumerable<T>
The records, enumerated once.
streamStream
The stream.
optionsCsvWriterOptions?
The options of the writer, or null for those of T.
leaveOpenbool
Whether to leave the stream open; it is flushed either way.

Returns

long
The number of records written, not counting the header.

Exceptions

ArgumentNullException
records or stream is null.
InvalidOperationException
T has nothing to write, or its records cannot be written.

View source

public static long WriteCsv<T>(this IEnumerable<T> records, TextWriter textWriter, CsvWriterOptions? options = null, bool leaveOpen = false)

Writes records to a text writer, with a header (unless the options say otherwise).

Type parameters

T
The type of the records.

Parameters

recordsIEnumerable<T>
The records, enumerated once.
textWriterTextWriter
The text writer.
optionsCsvWriterOptions?
The options of the writer, or null for those of T.
leaveOpenbool
Whether to leave the text writer open; it is flushed either way.

Returns

long
The number of records written, not counting the header.

Exceptions

ArgumentNullException
records or textWriter is null.
InvalidOperationException
T has nothing to write, or its records cannot be written.

View source

public static ValueTask<long> WriteCsvAsync<T>(this IAsyncEnumerable<T> records, CsvWriter writer, CancellationToken cancellationToken = default)

Writes an asynchronous sequence of records to a CSV writer asynchronously. The writer stays open.

Type parameters

T
The type of the records.

Parameters

recordsIAsyncEnumerable<T>
The records, enumerated once.
writerCsvWriter
The writer.
cancellationTokenCancellationToken
Cancels reading the records and writing.

Returns

ValueTask<long>
The number of records written, not counting the header.

Exceptions

ArgumentNullException
records or writer is null.
InvalidOperationException
T has nothing to write, or its records cannot be written.

View source

public static ValueTask<long> WriteCsvAsync<T>(this IEnumerable<T> records, CsvWriter writer, CancellationToken cancellationToken = default)

Writes records to a CSV writer asynchronously, with a header first when nothing has been written yet and its options have one. Records that can be read asynchronously are. The writer stays open; full buffers are written out.

Type parameters

T
The type of the records.

Parameters

recordsIEnumerable<T>
The records, enumerated once.
writerCsvWriter
The writer.
cancellationTokenCancellationToken
Cancels reading the records and writing.

Returns

ValueTask<long>
The number of records written, not counting the header.

Exceptions

ArgumentNullException
records or writer is null.
InvalidOperationException
T has nothing to write, or its records cannot be written.

View source

public static ValueTask<long> WriteCsvAsync<T>(this IAsyncEnumerable<T> records, string path, CsvWriterOptions? options = null, CancellationToken cancellationToken = default)

Writes an asynchronous sequence of records to a new CSV file asynchronously, replacing any file at path.

Type parameters

T
The type of the records.

Parameters

recordsIAsyncEnumerable<T>
The records, enumerated once.
pathstring
The path of the file.
optionsCsvWriterOptions?
The options of the writer, or null for those of T.
cancellationTokenCancellationToken
Cancels reading the records and writing the file.

Returns

ValueTask<long>
The number of records written, not counting the header.

Exceptions

ArgumentNullException
records or path is null.
InvalidOperationException
T has nothing to write, or its records cannot be written.

View source

public static ValueTask<long> WriteCsvAsync<T>(this IEnumerable<T> records, string path, CsvWriterOptions? options = null, CancellationToken cancellationToken = default)

Writes records to a new CSV file asynchronously, replacing any file at path. Records that can be read asynchronously (the results of CSV queries and of Entity Framework Core) are.

Type parameters

T
The type of the records.

Parameters

recordsIEnumerable<T>
The records, enumerated once.
pathstring
The path of the file.
optionsCsvWriterOptions?
The options of the writer, or null for those of T.
cancellationTokenCancellationToken
Cancels reading the records and writing the file.

Returns

ValueTask<long>
The number of records written, not counting the header.

Exceptions

ArgumentNullException
records or path is null.
InvalidOperationException
T has nothing to write, or its records cannot be written.

View source

public static ValueTask<long> WriteCsvAsync<T>(this IAsyncEnumerable<T> records, Stream stream, CsvWriterOptions? options = null, bool leaveOpen = false, CancellationToken cancellationToken = default)

Writes an asynchronous sequence of records to a stream asynchronously, as UTF-8.

Type parameters

T
The type of the records.

Parameters

recordsIAsyncEnumerable<T>
The records, enumerated once.
streamStream
The stream.
optionsCsvWriterOptions?
The options of the writer, or null for those of T.
leaveOpenbool
Whether to leave the stream open; it is flushed either way.
cancellationTokenCancellationToken
Cancels reading the records and writing the stream.

Returns

ValueTask<long>
The number of records written, not counting the header.

Exceptions

ArgumentNullException
records or stream is null.
InvalidOperationException
T has nothing to write, or its records cannot be written.

View source

public static ValueTask<long> WriteCsvAsync<T>(this IEnumerable<T> records, Stream stream, CsvWriterOptions? options = null, bool leaveOpen = false, CancellationToken cancellationToken = default)

Writes records to a stream asynchronously, as UTF-8. Records that can be read asynchronously are.

Type parameters

T
The type of the records.

Parameters

recordsIEnumerable<T>
The records, enumerated once.
streamStream
The stream.
optionsCsvWriterOptions?
The options of the writer, or null for those of T.
leaveOpenbool
Whether to leave the stream open; it is flushed either way.
cancellationTokenCancellationToken
Cancels reading the records and writing the stream.

Returns

ValueTask<long>
The number of records written, not counting the header.

Exceptions

ArgumentNullException
records or stream is null.
InvalidOperationException
T has nothing to write, or its records cannot be written.

View source