我是c#中的新成员,也使用csv文件。我需要使用一系列日期来过滤csv文件。在csv文件中包含csv文件中的一个范围之间的日期的过滤器行#
csv文件的内容与下面的数据类似。
“EmployeeCode”, “日期”, “时间”, “类型”
“3434”, “2013年1月22日”, “07:54”, “0”
“3023”,“01/23/2014“,”07:54“,”0“
”2897“,”01/24/2015“,”07:54“,”0“
”3734“,”01/25/2015 “07:54”,“0”
“3168”,“01/26/2015”,“07:54”,“0”
“4863”,“01/26/2015”,“07 :55“,”0“
”2513“,”01/27/2015“,”07:55“,”0“
”2582“,”01/27/2015“,”07:55“, “0”
这应该是输出:
“EmployeeCode”, “日期”, “时间”, “类型”
“3734”, “2015年1月25日”, “07:54”, “0”
“3168”,“01/26/2015”,“07:54”,“0”
“4863”,“01/26/2015”,“07:55”,“0”
“2513 ”, “2015年1月27日”, “7点55分”, “0”
“2582”, “2015年1月27日”, “7点55分”, “0”
壳牌可以做这,它的不公平,C#不能。因为在向网络搜索这类问题之后,找不到解决方案。
试一试这段代码。
DateTime dFm = new DateTime(2015, 01, 25);
DateTime dTo = new DateTime(2015, 01, 27);
var lines = System.IO.File.ReadAllLines(txtFileName.Text);
var data = lines
.Skip(1)
.Select(x => new
{
EmpCode = Int32.Parse(x.Split(new string[] { @""",""", @""""
}, StringSplitOptions.RemoveEmptyEntries)[0]),
Date = DateTime.ParseExact(
string.Concat(x.Split(new string[] { @""",""", @"""" },
StringSplitOptions.RemoveEmptyEntries)[1], " ",
x.Split(new string[] { @""",""", @"""" },
StringSplitOptions.RemoveEmptyEntries)[2]),
"MM/dd/yyyy HH:mm",
System.Globalization.CultureInfo.InvariantCulture),
Type = x.Split(new string[] { @""",""", @"""" },
StringSplitOptions.RemoveEmptyEntries)[3]
})
.Where(x => x.Date >= dFm && x.Date <= dTo)
.ToList();
}
你是怎么认为C#不能的?你试着写什么代码? –
C#可以,你不能:) –
嘿嘿,是像我这样的初学者。 – LovExpert