Search code examples
c#choetl

ChoETL to compare 2 CSV files


I'm trying to compare 2 CSV files and put after if the line is new, changed or deleted. There is a straight forward exemple with the library ChoETL.

I'm copying exactly the code from the exemple which is this :

    static void Main(string[] args)
    {
        //DoSomething();

        string csv1 = @"ID,name
        1,Danny
        2,Fred
        3,Sam";

        string csv2 = @"ID,name
        1,Danny
        3,Pamela
        4,Fernando";

        string desktopPath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);

        var r1 = ChoCSVReader.LoadText(csv1).WithFirstLineHeader().WithMaxScanRows(1).OfType<ChoDynamicObject>();
        var r2 = ChoCSVReader.LoadText(csv2).WithFirstLineHeader().WithMaxScanRows(1).OfType<ChoDynamicObject>();

        using (var w = new ChoCSVWriter(Console.Out).WithFirstLineHeader())
        {
            foreach (var t in r1.Compare(r2, "ID", "name"))
            {
                dynamic v1 = t.MasterRecord as dynamic;
                dynamic v2 = t.DetailRecord as dynamic;
                if (t.Status == CompareStatus.Unchanged || t.Status == CompareStatus.Deleted)
                {
                    v1.Status = t.Status.ToString();
                    w.Write(v1);
                }
                else
                {
                    v2.Status = t.Status.ToString();
                    w.Write(v2);
                }
            }
        }

        Console.ReadLine();
    }

The result should be : ID,name,Status 1,Danny,Unchanged 2,Fred,Deleted 3,Pamela,Changed 4,Fernando,New

But instead I get ID,name;Status 1,Danny;New 2,Fred;New 3,Sam;New 1,Danny;Deleted 3,Pamela;Deleted 4,Fernando;Deleted

I'm using the last version of ChoETL and Net Framework 4.7.2.


Solution

  • Figured out that your local culture is different, hence the system csv field separator may be different than ','.

    You will have to explicitly specify the field delimiter to reader by using WithDelimiter(",") method.

    Sample fiddle: https://dotnetfiddle.net/GdmMkQ

    Here is the reference git issue url https://github.com/Cinchoo/ChoETL/issues/276