如何使用 C# 过滤 CSV
How to filter a CSV with C#
我有一个包含 2 列的 csv,第一列包含字符串,第二列包含开始和结束时间(例如:11:00-22:00)。例如,您将如何尽可能高效地过滤包含 14:00 时间的字段?
例如:
餐厅名称营业时间
串鹤 11:30-21:00
大阪屋餐厅11:30-21:00
臭玫瑰9:00-22:00
我写 21:30 会显示 The Stinking Rose,或者如果我写 11:00 在控制台上显示 Kushi Tsuru、Osakaya Restaurant 和 The Stinking Rose。
class Filter
{
static void Main(string[] args)
{
List<Restaurant> csvFile = File.ReadAllLines(@"C:\restaurant-hours.csv")
.Skip(1)
.Select(Restaurant.FromCsv)
.ToList();
void filter_by_hour()
{
Console.WriteLine("\nEnter the hour");
string Dateofbirth = (Console.ReadLine();
var filter_data = csvFile.Where(e => (DateTime.Parse(e.OpenHour)) > Dateofbirth)
.Select(e => e);
}
}
}
这比 LINQ 和 Lambda 函数更快。此外,可以删除许多变量,只是为了清楚起见才添加它们。已经测试,有效。
static void Main(string[] args)
{
StreamReader reader = new StreamReader("a.txt");
string[] time = Console.ReadLine().Split(':');
int hour = 0;
int minute = 0;
try
{
hour = Convert.ToInt32(time[0]);
minute = Convert.ToInt32(time[1]);
}
catch
{
Console.WriteLine("The input must be something like aa:bb, with aa > 00 and aa < 24, bb >= 00 and bb <=53");
return;
}
string[] first_time;
string[] second_time;
string[] time_frame;
int first_hour;
int first_minute;
int second_hour;
int second_minute;
DateTime _date = new DateTime(1, 1, 1, hour, minute, 0);
DateTime first_date, second_date;
string line = "";
string[] tokens;
while ((line = reader.ReadLine()) != null)
{
tokens = line.Split(' ');
time_frame = tokens[tokens.Length - 1].Split('-');
first_time = time_frame[0].Split(':');
first_hour = Convert.ToInt32(first_time[0]);
first_minute = Convert.ToInt32(first_time[1]);
first_date = new DateTime(1, 1, 1, first_hour, first_minute, 0);
second_time = time_frame[1].Split(':');
second_hour = Convert.ToInt32(second_time[0]);
second_minute = Convert.ToInt32(second_time[1]);
second_date = new DateTime(1, 1, 1, second_hour, second_minute, 0);
if (_date >= first_date && _date <= second_date)
{
for (int i = 0; i < tokens.Length - 1; i++)
{
Console.Write(tokens[i]);
Console.Write(" ");
}
Console.WriteLine("");
}
}
reader.Close();
}
我有一个包含 2 列的 csv,第一列包含字符串,第二列包含开始和结束时间(例如:11:00-22:00)。例如,您将如何尽可能高效地过滤包含 14:00 时间的字段?
例如:
餐厅名称营业时间
串鹤 11:30-21:00
大阪屋餐厅11:30-21:00
臭玫瑰9:00-22:00
我写 21:30 会显示 The Stinking Rose,或者如果我写 11:00 在控制台上显示 Kushi Tsuru、Osakaya Restaurant 和 The Stinking Rose。
class Filter
{
static void Main(string[] args)
{
List<Restaurant> csvFile = File.ReadAllLines(@"C:\restaurant-hours.csv")
.Skip(1)
.Select(Restaurant.FromCsv)
.ToList();
void filter_by_hour()
{
Console.WriteLine("\nEnter the hour");
string Dateofbirth = (Console.ReadLine();
var filter_data = csvFile.Where(e => (DateTime.Parse(e.OpenHour)) > Dateofbirth)
.Select(e => e);
}
}
}
这比 LINQ 和 Lambda 函数更快。此外,可以删除许多变量,只是为了清楚起见才添加它们。已经测试,有效。
static void Main(string[] args)
{
StreamReader reader = new StreamReader("a.txt");
string[] time = Console.ReadLine().Split(':');
int hour = 0;
int minute = 0;
try
{
hour = Convert.ToInt32(time[0]);
minute = Convert.ToInt32(time[1]);
}
catch
{
Console.WriteLine("The input must be something like aa:bb, with aa > 00 and aa < 24, bb >= 00 and bb <=53");
return;
}
string[] first_time;
string[] second_time;
string[] time_frame;
int first_hour;
int first_minute;
int second_hour;
int second_minute;
DateTime _date = new DateTime(1, 1, 1, hour, minute, 0);
DateTime first_date, second_date;
string line = "";
string[] tokens;
while ((line = reader.ReadLine()) != null)
{
tokens = line.Split(' ');
time_frame = tokens[tokens.Length - 1].Split('-');
first_time = time_frame[0].Split(':');
first_hour = Convert.ToInt32(first_time[0]);
first_minute = Convert.ToInt32(first_time[1]);
first_date = new DateTime(1, 1, 1, first_hour, first_minute, 0);
second_time = time_frame[1].Split(':');
second_hour = Convert.ToInt32(second_time[0]);
second_minute = Convert.ToInt32(second_time[1]);
second_date = new DateTime(1, 1, 1, second_hour, second_minute, 0);
if (_date >= first_date && _date <= second_date)
{
for (int i = 0; i < tokens.Length - 1; i++)
{
Console.Write(tokens[i]);
Console.Write(" ");
}
Console.WriteLine("");
}
}
reader.Close();
}