不迭代 foreach 循环,但计数为 1849,应该迭代所有
我正在使用 CsvHelper 读取 CSV 文件。
这是我的代码(非常简单):
using (var reader = new StreamReader("example.csv"))
using (var csv = new CsvReader(reader, CultureInfo.InvariantCulture))
{
var records = csv.GetRecords<CsvData>();
int i = 0;
foreach (var record in records)
{
i++;
Console.WriteLine($"Processed {i}/{records.Count()} records.");
}
}
Console.WriteLine("Script finished");
问题是我的代码没有循环 foreach,所以它不会打印任何内容...我在 i++;
行中放置了一个断点,但它没有t 中断。
如果我打印 records.Count()
它将返回 3:
这可能是 CSV 文件的示例:
代码格式,以便您可以复制它:
Size,Color
8,Yellow
2,Orange
13,Blue
这可能是 CsvData 类的示例:
public class CsvData
{
public decimal? Size { get; set; }
public string Color { get; set; }
}
如何将我的行解析迭代到我的 CsvData 类中,创建一个List
或类似的?
I'm using CsvHelper to read a CSV file.
This is my code (pretty simple):
using (var reader = new StreamReader("example.csv"))
using (var csv = new CsvReader(reader, CultureInfo.InvariantCulture))
{
var records = csv.GetRecords<CsvData>();
int i = 0;
foreach (var record in records)
{
i++;
Console.WriteLine(quot;Processed {i}/{records.Count()} records.");
}
}
Console.WriteLine("Script finished");
The problem is that my code is not looping that foreach, so it won't print anything... I put a breakpoint in i++;
line but it doesn't breaks.
If I print records.Count()
it will return 3:
This could be an example of a CSV file:
Code format so you can copy it:
Size,Color
8,Yellow
2,Orange
13,Blue
And this could be an example of class CsvData:
public class CsvData
{
public decimal? Size { get; set; }
public string Color { get; set; }
}
How should I iterate my rows parsing into my CsvData class creating a List<CsvData>
or similar?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
@Joel Coehoorn 是正确的。一旦您调用
.Count()
,您就告诉CsvHelper
读取整个 CSV 文件以找出文件中有多少条记录。您现在位于数据流的末尾,并且没有更多记录可供读取。调用.ToList()
会做同样的事情。它读取整个 CSV 文件,但这次它将记录保存到内存的records
变量中。如果您的文件较小,这很好,但如果您的文件非常大,则可能会遇到内存问题。根据入门说明
选项 1
您已经发现可以调用
ListRecords = csv.GetRecords().ToList();
并将所有记录放入内存中。只要明白这就是你正在做的事情。我还将您的计数放入变量var count =records.count();
,而不是让您的代码每次循环通过List
来获取计数。选项 2
不要一开始就获取计数。最后只给出总数即可。
选项 3
循环文件两次。一次获取计数,第二次获取数据。
@Joel Coehoorn is correct. As soon as you call
.Count()
, you have just toldCsvHelper
to read the whole CSV file in order to find out how many records there are in the file. You are now at the end of the data stream and there are no more records left to read. Calling.ToList()
does the same thing. It reads the whole CSV file, but this time it saves the records to memory in therecords
variable. This is fine if your file is smaller, but you could run into memory issues if you have a very large file.Per the Getting Started Instructions
Option 1
You have already discovered that you can call
List<CsvData> records = csv.GetRecords<CsvData>().ToList();
and bring all the records into memory. Just understand that is what you are doing. I would also put your count into a variablevar count = records.count();
instead of making your code loop through theList<CsvData>
each time to get the count.Option 2
Don't get the count at the beginning. Just give a total at the end.
Option 3
Loop through the file twice. Once to get the count and the 2nd time to get the data.
将集合转换为列表有效:
只是:
代码结果(适合懒人)
Converting the collection to list worked:
Just by:
Result of code (for lazy people)