.Net CSVHelper 访问所有记录

我对如何访问所有记录有点困惑。它不会让我通过项目运行 foreach() 并且它只返回第一个作为对象。我知道它在文档中这么说。但我不知道该怎么办。


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. Be careful though. If you do anything that 

executes a LINQ projection, such as calling .ToList(), the entire file will 

be read into memory. CsvReader is forward only, so if you want to run any 

LINQ queries against your data, you'll have to pull the whole file into 

memory. Just know that is what you're doing.

这是我的代码


protected void WatcherCreated(object sender, FileSystemEventArgs e)

    {


        Console.WriteLine("Watcher Created");

        string[] files = Directory.GetFiles(_watchedFolder, "*.csv");

        foreach (string file in files)

        {

            using (var reader = new StreamReader(file))

            using (var csv = new CsvReader(reader))

            {

                try

                {

                    csv.Read();

                    var records = csv.GetRecord<InputCsv>();



                }

                catch (CsvHelper.HeaderValidationException exception)

                {

                    Console.WriteLine(exception);

                    throw;

                }





            }

        }

    }

记录是假定的 IEnumerable .. 我最终想使用对象 o 使用 Newtonsoft.Json 将其解析为 json


在 csv.GetRecords(); 之后,它甚至没有为我提供 .ToList() 选项。


HUH函数
浏览 89回答 1
1回答

侃侃尔雅

您在函数调用中缺少一个字母:var&nbsp;records&nbsp;=&nbsp;csv.GetRecord<InputCsv>();你应该打电话GetRecords- 注意额外的 s。var&nbsp;records&nbsp;=&nbsp;csv.GetRecords<InputCsv>();
打开App,查看更多内容
随时随地看视频慕课网APP