CsvRecordExtensionToDictionary Method

Definition

Namespace: FolkerKinzel.CsvTools
Assembly: FolkerKinzel.CsvTools (in FolkerKinzel.CsvTools.dll) Version: 2.0.1+2345335399184346d9b2cc992ed5c814406052c1
C#
public static Dictionary<string, ReadOnlyMemory<char>> ToDictionary(
	this CsvRecord record
)

Parameters

record  CsvRecord
The CsvRecord instance to convert.

Return Value

DictionaryString, ReadOnlyMemoryChar
A copy of the data stored in record as Dictionary<string, ReadOnlyMemory<char>>.

Usage Note

In Visual Basic and C#, you can call this method as an instance method on any object of type CsvRecord. When you use instance method syntax to call this method, omit the first parameter. For more information, see Extension Methods (Visual Basic) or Extension Methods (C# Programming Guide).

Remarks

The method copies the data stored in record into a newly created Dictionary<string, ReadOnlyMemory<char>> instance, which uses the same StringComparer for key comparison that was used to create record.

Example

  Note

In the following code examples - for easier readability - exception handling has been omitted.

Saving a CSV file:

C#
using System.Text;
using FolkerKinzel.CsvTools;

namespace Examples;

internal static class CsvAnalyzerExample
{
    public static void ParseForeignCsvFile(string filePath)
    {
        const string nonStandardCsv = """


            First # "Second # Column"   
            1,"2",3 # "Get's
            too much" # LOST?

            too few


            """;

        File.WriteAllText(filePath, nonStandardCsv, Encoding.Unicode);

        using CsvReader csv = Csv.OpenReadAnalyzed(filePath);
        CsvRecord[] data = [.. csv];

        using (CsvWriter writer = Csv.OpenWrite(filePath, data[0].ColumnNames))
        {
            foreach (CsvRecord record in data)
            {
                writer.Record.FillWith(record.Values);
                writer.WriteRecord();
            }
        }

        Console.WriteLine(File.ReadAllText(filePath));
    }
}

/*
 Console output:

Column1,Column2,Column3
First ,Second # Column,
"1,""2"",3 ","Get's
too much", LOST?
too few,,

*/

See Also