C# .net core 3.1 读取一个文件两次 CsvHelper

C# .net core 3.1 read a file twice CsvHelper

我正在尝试使用 C# NET CORE 3.1 读取 csv 文件两次。我有 100k 记录的限制,如果超过这个我应该 return 一个错误,否则,我应该处理文件。我想在实际处理(第二次读取)之前获取记录数,以便用户在记录超过限制时得到快速响应。 以下代码不起作用,因为我们只能读取一次流。有什么方法可以让我们读取它两次,以便给定的代码正常工作?

private IList<TObject> TryReadCsv(IFormFile file)
{
    int maxRecordsLimit = 100000;
    var result = new List<TObject>();
    try
    {
        using StreamReader reader = new StreamReader(file.OpenReadStream(), Encoding.UTF8);
        var config = new CsvConfiguration(CultureInfo.InvariantCulture) { Delimiter = ",", Encoding = Encoding.UTF8 };
        using var csv = new CsvReader(reader, config);
        
        // First read; light read to get the number of records without any heavy processing
        int recordsCount = 0;
        while (csv.Read())
        {
            if (recordsCount > MaxRecordsLimit)
            {
                // Return the error from here that the number of records are more than the maxRecordsLimit
            }
            recordsCount++;
        }
        
        
        // Second read for processing; heavy read
        while (csv.Read())
        {
            // Do some heavy processing which takes some more time
        }
        

        return result;
    }
    catch (CsvHelperException ex)
    {
        // catch exception
    }
}

正如@jdweng 回答的那样,我需要将 reader 位置设置为零,因为:

reader.BaseStream.Position = 0;