C# 选择日期 - 显示与文本文件中的日期关联的数据
C# Choose Date - Display Data associated with Date from Text File
问题: weather.txt 包含 2018 年 1 月每一天的天气信息。每行的格式为:
"Date;Precipitation;HighTemp;LowTemp"
1/1/2018;0;29;10
对每一行进行拆分和标记化,并显示在表单标签中选择的日期的数据。
我遇到的问题是如何检查标记化的 DateTime 数据以查看它是否与从 DateTimePicker 中选择的 DateTime 相匹配,以便我可以在标签中显示正确的信息。
表单中用于保存相应数据的标签:dateLabel、precipLable、highLabel、lowLabel。
namespace WeatherData2
{
struct WeatherData
{
public DateTime date;
public double precip;
public int highTemp;
public int lowTemp;
}
public partial class Form1 : Form
{
private List<WeatherData> weatherList = new List<WeatherData>();
public Form1()
{
InitializeComponent();
}
private void ReadFile()
{
try
{
StreamReader inputFile;
string line;
WeatherData entry = new WeatherData();
char[] delim = { ';' };
inputFile = File.OpenText("weather.txt");
while (!inputFile.EndOfStream)
{
line = inputFile.ReadLine();
string[] tokens = line.Split(delim);
DateTime.TryParse(tokens[0], out entry.date);
double.TryParse(tokens[1], out entry.precip);
int.TryParse(tokens[2], out entry.highTemp);
int.TryParse(tokens[3], out entry.lowTemp);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
{
}
}
}
如果需要任何说明,请告诉我。我大概写了那10种迷惑。任何帮助将不胜感激。谢谢
您可以使用 linq 轻松检查:
private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
{
var pickedDate = dateTimePicker1.Value.ToShortDateString();
if (weatherList.Any(e => e.date == pickedDate))
{
var matching = weatherList.First(e => e.date == pickedDate);
}
}
您还需要对循环进行一些更改。现在,您正在为循环的每次迭代覆盖您的条目对象,而不是将其添加到您的集合中。
while (!inputFile.EndOfStream)
{
//create a new instance of entry for each iteration
entry = new WeatherData();
line = inputFile.ReadLine();
string[] tokens = line.Split(delim);
DateTime.TryParse(tokens[0], out entry.date);
double.TryParse(tokens[1], out entry.precip);
int.TryParse(tokens[2], out entry.highTemp);
int.TryParse(tokens[3], out entry.lowTemp);
//add the item to the list
weatherList.Add(entry);
}
问题: weather.txt 包含 2018 年 1 月每一天的天气信息。每行的格式为:
"Date;Precipitation;HighTemp;LowTemp"
1/1/2018;0;29;10
对每一行进行拆分和标记化,并显示在表单标签中选择的日期的数据。
我遇到的问题是如何检查标记化的 DateTime 数据以查看它是否与从 DateTimePicker 中选择的 DateTime 相匹配,以便我可以在标签中显示正确的信息。
表单中用于保存相应数据的标签:dateLabel、precipLable、highLabel、lowLabel。
namespace WeatherData2
{
struct WeatherData
{
public DateTime date;
public double precip;
public int highTemp;
public int lowTemp;
}
public partial class Form1 : Form
{
private List<WeatherData> weatherList = new List<WeatherData>();
public Form1()
{
InitializeComponent();
}
private void ReadFile()
{
try
{
StreamReader inputFile;
string line;
WeatherData entry = new WeatherData();
char[] delim = { ';' };
inputFile = File.OpenText("weather.txt");
while (!inputFile.EndOfStream)
{
line = inputFile.ReadLine();
string[] tokens = line.Split(delim);
DateTime.TryParse(tokens[0], out entry.date);
double.TryParse(tokens[1], out entry.precip);
int.TryParse(tokens[2], out entry.highTemp);
int.TryParse(tokens[3], out entry.lowTemp);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
{
}
}
}
如果需要任何说明,请告诉我。我大概写了那10种迷惑。任何帮助将不胜感激。谢谢
您可以使用 linq 轻松检查:
private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
{
var pickedDate = dateTimePicker1.Value.ToShortDateString();
if (weatherList.Any(e => e.date == pickedDate))
{
var matching = weatherList.First(e => e.date == pickedDate);
}
}
您还需要对循环进行一些更改。现在,您正在为循环的每次迭代覆盖您的条目对象,而不是将其添加到您的集合中。
while (!inputFile.EndOfStream)
{
//create a new instance of entry for each iteration
entry = new WeatherData();
line = inputFile.ReadLine();
string[] tokens = line.Split(delim);
DateTime.TryParse(tokens[0], out entry.date);
double.TryParse(tokens[1], out entry.precip);
int.TryParse(tokens[2], out entry.highTemp);
int.TryParse(tokens[3], out entry.lowTemp);
//add the item to the list
weatherList.Add(entry);
}