读取.csv文件并计算日期范围之间的行数C# [英] Reading in a .csv file and counting the number of lines between date ranges C#

查看:34
本文介绍了读取.csv文件并计算日期范围之间的行数C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用CSVhelper读取CSV文件时,我有成千上万的数据行,每行的第5列中都有日期/时间.我想计算2个给定日期之间的行数.如果我们看下面的输入:

When reading in a CSV file with CSVhelper I have thousands of rows of data, the 5th column of each row will have a date/time in it. I want to count the number of lines between 2 given dates. If we look at the input below:

代码需要对2014-10-24和2014-10-29之间的行进行计数,并将此计数保存为变量,在这种情况下,预期输出为(9).请注意,这是一个示例输入,日期范围之间的实际行数将非常可观.

The code needs to count the lines between say 2014-10-24 and 2014-10-29 and save this count as a variable, in this case, expected output will be (9). Please note this is a sample input and the actual amount of lines between date ranges will be considerable.

当前.csv文件正在通过csvreader读取并存储在: IEnumerable dataRecord = reader.GetRecords< dataRecord>().ToList();

Currently the .csv file is being read in through csvreader and storing in: IEnumerable dataRecord = reader.GetRecords<dataRecord>().ToList();

我很困惑如何解决此问题,我们将不胜感激.

I'm stumped as to how to go about this, any help would be appreciated.

推荐答案

尝试如下操作:

var count = dataRecord
    .SkipWhile(record => DateTime.Parse(record.Date).Date != new DateTime(2014, 10, 24))
    .TakeWhile(record => DateTime.Parse(record.Date).Date != new DateTime(2014, 10, 29))
    .Count();

这篇关于读取.csv文件并计算日期范围之间的行数C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆