CsvConverterToCsvTSource(IEnumerableTSource, CsvMapping, ActionTSource, Object, Char, IReadOnlyCollectionString) Method

Converts a collection of TSource instances to a CSV String with header row.

Definition

Namespace: FolkerKinzel.CsvTools.Mappings
Assembly: FolkerKinzel.CsvTools.Mappings (in FolkerKinzel.CsvTools.Mappings.dll) Version: 1.1.0+1263e8243dc2cd78095f678f813d7d9c52ea4315
C#
public static string ToCsv<TSource>(
	IEnumerable<TSource> data,
	CsvMapping mapping,
	Action<TSource, Object> conversion,
	char delimiter = ',',
	IReadOnlyCollection<string?>? columnNames = null
)

Parameters

data  IEnumerableTSource
The data to convert to CSV. Each item will be represented with a CSV row.
mapping  CsvMapping
The CsvMapping used to convert a TSource instance to a CSV row.
conversion  ActionTSource, Object

A method that fills the content of a TSource instance into the properties of mapping.

conversion is called with each CSV row to be written and it gets the TSource instance and mapping as arguments. mapping is passed to the method as dynamic argument: Inside the conversion method the registered DynamicProperty instances can be used like regular .NET properties, but without IntelliSense ("late binding").

With each call of conversion all DynamicProperty instances in mapping are reset to their DefaultValue.

delimiter  Char  (Optional)
The field separator character.
columnNames  IReadOnlyCollectionString  (Optional)

A collection of column names for the header to be written, or null to use the PropertyNames of mapping as column names.

The collection determines the order in which the columns appear in the CSV file.

The collection will be copied. If the collection contains null values, empty strings, or white space, these are replaced by automatically generated column names. Column names cannot appear twice. By default the comparison is case-sensitive but it will be reset to a case-insensitive comparison if the column names are also unique when treated case-insensitive.

Type Parameters

TSource
Generic type parameter for the data type to write as CSV row.

Return Value

String
A CSV String with header row that contains the contents of data.

Exceptions

ArgumentNullExceptiondata, or mapping, or conversion is null.
ArgumentException A column name in columnNames occurs twice.
IOExceptionI/O error.

See Also