Note
In the following code examples - for easier readability - exception handling
has been omitted.
public static string ToCsv<TSource>(
this IEnumerable<TSource> data,
CsvMapping mapping,
Action<TSource, Object> conversion,
char delimiter = ',',
IReadOnlyCollection<string?>? columnNames = null
)
<ExtensionAttribute>
Public Shared Function ToCsv(Of TSource) (
data As IEnumerable(Of TSource),
mapping As CsvMapping,
conversion As Action(Of TSource, Object),
Optional delimiter As Char = ","C,
Optional columnNames As IReadOnlyCollection(Of String) = Nothing
) As String
public:
[ExtensionAttribute]
generic<typename TSource>
static String^ ToCsv(
IEnumerable<TSource>^ data,
CsvMapping^ mapping,
Action<TSource, Object^>^ conversion,
wchar_t delimiter = L',',
IReadOnlyCollection<String^>^ columnNames = nullptr
)
[<ExtensionAttribute>]
static member ToCsv :
data : IEnumerable<'TSource> *
mapping : CsvMapping *
conversion : Action<'TSource, Object> *
?delimiter : char *
?columnNames : IReadOnlyCollection<string>
(* Defaults:
let _delimiter = defaultArg delimiter ','
let _columnNames = defaultArg columnNames null
*)
-> string
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.
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.
Object serialization with CSV:
using FolkerKinzel.CsvTools;
using FolkerKinzel.CsvTools.Mappings;
using FolkerKinzel.CsvTools.Mappings.TypeConverters;
namespace Benchmarks;
internal static partial class CalculationWriter
{
internal static string WriteDefault(Calculation[] data)
{
var doubleConverter = new DoubleConverter();
CsvMapping mapping = CsvMappingBuilder
.Create()
.AddProperty("First", doubleConverter)
.AddProperty("Operator", new CharConverter())
.AddProperty("Second", doubleConverter)
.AddProperty("Result", doubleConverter)
.Build();
return data.ToCsv(
mapping,
static (calculation, mapping) =>
{
mapping.First = calculation.First;
mapping.Operator = calculation.Operator;
mapping.Second = calculation.Second;
mapping.Result = calculation.Result;
});
}
}
ArgumentNullException | data, or mapping, or conversion is null. |
ArgumentException | A column name in columnNames occurs twice. |
IOException | I/O error. |