Search code examples
c#csvhelper

CsvHelper Error during debug - System.Linq.SystemCore_EnumerableDebugView


Doing initial testing of CsvHelper in Visual Studio 2019 with .NET Core 2.2.

I have a test csv file which has headers names which contain parentheses. I'm using PrepareHeaderForMatch to remove the parentheses from the names so they match my class property names.

During debug, I'm getting the following error:

Error Evaluation of method System.Linq.SystemCore_EnumerableDebugView`1[PIlibrary.Entity].get_Items() calls into native method Interop+Kernel32.ReadFile(System.Runtime.InteropServices.SafeHandle, byte*, int, int&, System.IntPtr). Evaluation of native methods in this context is not supported.

Here is the code:

public static void Main(string[] args)
{
    using (var reader = new StreamReader(@"C:\Users\me\Desktop\SD Temp\Data_Weekly\pfilerr.csv"))
    using (var csv = new CsvReader(reader))
    {
        csv.Configuration.PrepareHeaderForMatch = (string header, int index) =>
            header.Replace("(", "").Replace(")", "");
        var records = csv.GetRecords<Entity>();
    }
}

The csv file can be found here, for reference: csv file It's only 20 rows, and less than half of the 300+ columns are populated.

The Entity class has over 300 properties (to match up to the over 300 columns) - I can post that here if needed but I've initially chosen not to due to its length. The names of the properties match the headers of the csv file, with the exception of certain names which contain parentheses as mentioned above.

I understand it's entirely possible this error is C# related, and not necessarily specific to CsvHelper - I'm not certain though, as this is the first of it I've encountered.


Solution

  • The issue is that when you look at the records in Debug, you are looking at an IEnumerable<Entity> which only yield the records as you iterate them. Debug is unable to iterate them for you. CsvHelper Getting Started

    The GetRecords<T> method will return an IEnumerable<T> that will yield records. What this means is that only a single record is returned at a time as you iterate the records. That also means that only a small portion of the file is read into memory.

    Calling ToList()or otherwise iterating the records pulls them into memory

    var records = csv.GetRecords<Entity>().ToList();