C# 如何读取前31行并获取每行C的第三个元素?
我必须阅读两个分布不同的文件。如果第一个元素与第二个元素相同,我还需要进行比较。没有数字,只是是否№ 一个文件中的站点的名称与相同№ 在另一个文件中放置两个文件的第一个元素,然后我需要比较同一年开始和结束的年份。 然后你需要在combobox1中充电№ 从第一个文件的开始到结束的2年内的站点和combobox2。 然后我需要从第一个文件中读取所选年份的31行 我不知道为什么combobox2只填写一年而不是所有年份 我创建类数据条目:C# 如何读取前31行并获取每行C的第三个元素?,c#,.net,winforms,file,readfile,C#,.net,Winforms,File,Readfile,我必须阅读两个分布不同的文件。如果第一个元素与第二个元素相同,我还需要进行比较。没有数字,只是是否№ 一个文件中的站点的名称与相同№ 在另一个文件中放置两个文件的第一个元素,然后我需要比较同一年开始和结束的年份。 然后你需要在combobox1中充电№ 从第一个文件的开始到结束的2年内的站点和combobox2。 然后我需要从第一个文件中读取所选年份的31行 我不知道为什么combobox2只填写一年而不是所有年份 我创建类数据条目: using System; using System.Col
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace WindowsFormsApp1
{
public class DataEntry
{
public string Station { get; set; }
//2. item
public string Year
{
get;
set;
}
//3. item
public string Number
{
get;
set;
}
//4. item
public string January
{
get;
set;
}
//5. item
public string February
{
get;
set;
}
//6. item
public string March
{
get;
set;
}
//7. item
public string April
{
get;
set;
}
//8. item
public string Mai
{
get;
set;
}
//9. item
public string Juni
{
get;
set;
}
//10. item
public string Juli
{
get;
set;
}
//11. item
public string August
{
get;
set;
}
//12. item
public string September
{
get;
set;
}
//13. item
public string October
{
get;
set;
}
//14. item
public string November
{
get;
set;
}
//15. item
public string December
{
get;
set;
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DemoProject
{
public class DataEntry
{
public DataEntry()
{
}
//1. item
public string Station
{
get;
set;
}
//2. item
public string Year
{
get;
set;
}
//3. item
public string Number
{
get;
set;
}
//4. item
public string January
{
get;
set;
}
//5. item
public string February
{
get;
set;
}
//6. item
public string March
{
get;
set;
}
//7. item
public string April
{
get;
set;
}
//8. item
public string Mai
{
get;
set;
}
//9. item
public string Juni
{
get;
set;
}
//10. item
public string Juli
{
get;
set;
}
//11. item
public string August
{
get;
set;
}
//12. item
public string September
{
get;
set;
}
//13. item
public string October
{
get;
set;
}
//14. item
public string November
{
get;
set;
}
//15. item
public string December
{
get;
set;
}
}
}
创建第二个类后:数据文件
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace WindowsFormsApp1
{
public class DataFile
{
public DataFile()
{
this.Entries = new List<DataEntry>();
}
public List<DataEntry> Entries { get; }
public static DataFile Load(string[] allLines)
{
DataFile result = new DataFile();
// if (System.IO.File.Exists(fileName))
// {
// string[] allLines = System.IO.File.ReadAllLines(fileName);
DataEntry entry = new DataEntry();
foreach (var line in allLines)
{
var splittedValues = line.Split(',');
entry.Station = splittedValues[0];
entry.Year = splittedValues[1];
entry.Number = splittedValues[2];
entry.January = splittedValues[3];
entry.February = splittedValues[4];
entry.March = splittedValues[5];
entry.April = splittedValues[6];
entry.Mai = splittedValues[7];
entry.Juni = splittedValues[8];
entry.Juli = splittedValues[9];
entry.August = splittedValues[10];
entry.September = splittedValues[11];
entry.October = splittedValues[12];
entry.November = splittedValues[13];
entry.December = splittedValues[14];
}
result.Entries.Add(entry);
// }
return result;
}
/// <summary>
/// Returns true if all entries (all lines of the csv file) have the same station (the same first word).
/// If one entry have another first word it returns false.
/// </summary>
/// <returns></returns>
public bool CheckStation()
{
string station = string.Empty;
bool firstEntry = true;
foreach (var entry in this.Entries)
{
if (firstEntry)
{
station = entry.Station;
}
else
{
if (entry.Station != station)
{
return false;
}
}
}
return true;
}
/// <summary>
/// Returns the entries of the file which have the same year as given.
/// </summary>
/// <param name="year"></param>
/// <returns></returns>
public DataEntry[] GetAllEntriesOfOneYear(string year)
{
return this.Entries.Where(p => p.Year == year).ToArray();
}
/// <summary>
/// Returns true if this entries contains the same years as the entries of the given file
/// If one file have one more or less year it returns false
/// </summary>
/// <param name="otherFile"></param>
/// <returns></returns>
public bool CheckYears(DataFile otherFile)
{
return true;
string[] allPossibleYearsOfThisFile = this.GetYears();
string[] allPossibleYearsOfTheOtherFile = otherFile.GetYears();
if (allPossibleYearsOfThisFile.Length != allPossibleYearsOfTheOtherFile.Length)
{
return false;
}
else
{
foreach (var year in allPossibleYearsOfThisFile)
{
if (!allPossibleYearsOfTheOtherFile.Contains(year))
{
return false;
}
}
}
return true;
}
/// <summary>
/// Returns all possible years of the entries.
/// </summary>
/// <returns></returns>
public string[] GetYears()
{
// string[] years = new string[9999];
//
// foreach (var item in Entries)
// {
// years = item.Year;
// }
// return years;
return this.Entries.Select(p => p.Year).Distinct().ToArray();
}
}
}
表格中的代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SQLite;
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
private Service service;
DataFile firstFile;
DataFile secondFile;
public Form1()
{
service = new Service();
// firstFile = new DataFile();
// secondFile = new DataFile();
InitializeComponent();
}
private SQLiteConnection Connection;
string pathFolder;
string pathFolder2;
List<string> resultYears = new List<string>();
List<string> resultYears2 = new List<string>();
private void button1_Click(object sender, EventArgs e)
{
using (OpenFileDialog dialog = new OpenFileDialog())
{
if (dialog.ShowDialog(this) == DialogResult.OK)
{
string sFileName = dialog.FileName;
pathFolder = sFileName;
label3.Text = pathFolder;
label3.Show();
string[] lines = System.IO.File.ReadAllLines(dialog.FileName);
firstFile = DataFile.Load(lines);
if (!firstFile.CheckStation())
{
MessageBox.Show("Файла с дневни данни трябва да съдържа само една станция!");
}
}
}
}
private void button2_Click(object sender, EventArgs e)
{
using (OpenFileDialog dialog = new OpenFileDialog())
{
if (dialog.ShowDialog(this) == DialogResult.OK)
{
string sFileName = dialog.FileName;
pathFolder2 = sFileName;
label4.Text = pathFolder2;
label4.Show();
string[] lines = System.IO.File.ReadAllLines(dialog.FileName);
secondFile = DataFile.Load(lines);
if (!secondFile.CheckStation())
{
MessageBox.Show("Файла с месечни данни трябва съдържа само една станция!");
return;
}
}
}
}
private void button3_Click(object sender, EventArgs e)
{
if (firstFile != null && secondFile != null)
{
if (firstFile.Entries.FirstOrDefault().Station.ToString() != secondFile.Entries.FirstOrDefault().Station.ToString())
{
MessageBox.Show("Номера на станцията в единия файл не отговаря на номера на станцията в другият файл!" + Environment.NewLine + Environment.NewLine +
"ЗАБЕЛЕЖКА!" + Environment.NewLine + Environment.NewLine + "В двата файла, номера на станцията трябва да бъде един и същ!");
}
comboBox1.Items.Add(firstFile.Entries.First().Station);
List<string> years = new List<string>();
// if (firstFile.CheckYears(secondFile))
// {
comboBox2.Items.AddRange(service.GetAllYears(firstFile));
//comboBox2.Items.Add(firstFile.GetYears());
// }
// else
// {
// MessageBox.Show("Годините от двата файла не съвпадат.");
// }
}
else
{
MessageBox.Show("One or both files are empty. Please select the file and read the data first.");
}
}
public void loadEntries()
{
string selectedYear = this.comboBox2.SelectedValue.ToString();
DataEntry[] entries = firstFile.GetAllEntriesOfOneYear(selectedYear);
//TODO:
//Now you have all lines of the first file which have the selected year.
//With this list you can work
}
public void label3_Click(object sender, EventArgs e)
{
}
public void label4_Click(object sender, EventArgs e)
{
}
}
}
直到2015年,下一年的下降趋势也一样
第二个CSV文件:
18050,1976,1,0.390,0.660,0.290,0.740,9.160,1.400,0.670,3.120,0.460,0.420,0.360,0.400,
18050,1976,2,0.390,0.520,0.290,0.740,7.540,1.270,0.670,2.660,0.460,0.420,0.360,0.380,
18050,1976,3,0.390,0.450,0.240,0.660,5.260,1.270,0.670,2.510,0.460,0.420,0.410,0.400,
18050,1976,4,0.390,0.450,0.240,0.660,4.400,1.180,0.620,2.360,0.460,0.410,0.400,0.440,
18050,1976,5,0.390,0.450,0.290,0.660,4.220,1.080,0.620,2.360,0.460,0.410,0.400,4.750,
18050,1976,6,0.520,0.390,0.240,0.580,4.040,1.270,0.620,4.200,0.460,0.410,0.380,2.810,
18050,1976,7,0.390,0.390,0.240,0.520,3.680,37.800,0.620,5.870,0.460,0.400,0.360,1.620,
18050,1976,8,0.390,0.390,0.200,0.580,3.330,22.900,0.580,4.570,0.460,0.380,0.360,0.980,
18050,1976,9,0.390,0.390,0.200,0.660,2.830,11.200,0.580,4.020,0.460,0.360,0.360,0.740,
18050,1976,10,0.390,0.340,0.200,1.380,2.650,8.120,0.580,3.660,0.440,0.360,0.360,0.520,
18050,1976,11,0.340,0.390,0.200,2.260,2.350,5.870,0.580,3.270,0.440,0.360,0.360,0.460,
18050,1976,12,0.340,0.450,0.200,1.700,2.350,4.750,0.580,4.570,0.440,0.360,0.360,0.460,
18050,1976,13,0.340,0.390,0.200,1.590,2.350,3.840,0.540,4.020,0.440,0.340,0.360,0.440,
18050,1976,14,0.340,0.390,0.290,2.120,2.200,3.120,0.540,3.660,0.420,0.340,0.340,0.520,
18050,1976,15,0.290,0.390,0.290,2.400,2.050,2.970,0.540,3.270,0.420,0.400,0.340,0.520,
18050,1976,16,0.290,0.390,0.240,1.590,1.770,2.810,0.540,2.970,0.420,0.360,0.340,0.440,
18050,1976,17,0.290,0.340,0.290,1.170,1.520,2.660,0.540,2.660,0.410,0.360,0.330,0.420,
18050,1976,18,0.290,0.340,0.290,1.170,1.270,2.360,0.540,2.210,0.410,0.410,0.340,0.420,
18050,1976,19,0.240,0.340,0.390,1.170,1.080,2.210,0.540,2.060,0.410,0.410,0.400,0.410,
18050,1976,20,0.290,0.340,0.390,1.010,1.080,2.060,0.520,1.760,0.400,0.400,1.340,0.400,
18050,1976,21,0.290,0.290,0.390,0.920,1.270,1.760,0.520,1.200,0.740,0.400,2.660,0.400,
18050,1976,22,0.340,0.290,0.450,0.820,2.860,1.480,0.520,1.080,0.580,0.380,1.760,0.400,
18050,1976,23,0.340,0.290,0.520,0.740,3.050,1.200,0.520,0.980,0.580,0.380,0.980,0.400,
18050,1976,24,0.340,0.290,0.520,0.660,4.000,0.980,0.540,0.810,0.540,0.380,0.520,0.380,
18050,1976,25,0.340,0.290,0.920,0.740,2.680,0.890,2.810,0.670,0.520,0.360,0.460,0.380,
18050,1976,26,0.390,0.290,1.380,1.380,2.060,0.810,2.510,0.580,0.520,0.360,0.440,0.380,
18050,1976,27,0.740,0.290,1.490,2.570,1.770,0.810,2.510,0.580,0.490,0.360,0.420,0.380,
18050,1976,28,1.280,0.290,1.380,2.730,1.770,0.740,4.750,0.520,0.460,0.360,0.410,0.360,
18050,1976,29,1.010,0.290,1.090,3.610,1.650,0.740,5.480,0.520,0.420,0.360,0.410,0.360,
18050,1976,30,0.820,,0.820,4.000,1.520,0.670,4.210,0.490,0.420,0.360,0.400,0.360,
18050,1976,31,0.660,,0.740,,1.520,,3.660,0.460,,0.360,,0.440,
18050,1976,НМ,0.240,0.240,0.160,0.520,0.990,0.670,0.490,0.460,0.400,0.340,0.330,0.360,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
18050,1976,СР,0.440,0.370,0.480,1.380,2.880,4.340,1.280,2.380,0.470,0.380,0.560,0.700,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
18050,1976,НГ,1.380,0.660,1.560,4.640,9.800,53.500,6.270,6.270,0.810,0.420,2.810,5.300,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
18050,1976,,,,НМ,0.160,СР,1.310,НГ,53.500,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
18050,1977,НМ,0.320,1.030,0.540,0.360,0.240,0.360,0.340,0.120,0.100,0.140,0.120,0.140,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
18050,1977,СР,0.620,1.720,1.430,0.530,0.400,0.800,0.470,0.230,0.230,0.170,0.260,0.260,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
18050,1977,НГ,3.860,7.040,4.300,1.140,2.050,8.500,1.030,0.360,2.050,0.360,2.050,1.770,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
18050,1977,,,,НМ,0.100,СР,0.590,НГ,8.500,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
18050,1978,НМ,0.140,0.200,0.200,0.670,0.580,0.160,0.140,0.120,0.140,0.092,0.110,0.200,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
18050,1978,СР,0.250,0.420,0.720,1.090,0.710,0.370,0.250,0.200,0.240,0.170,0.130,0.630,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
18050,1978,НГ,1.260,3.140,5.340,2.950,1.480,0.770,0.510,0.510,1.070,0.270,0.300,3.450,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
18050,1978,,,,НМ,0.092,СР,0.430,НГ,5.340,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
18050,1979,НМ,0.090,0.209,0.175,0.175,0.580,0.540,0.485,0.433,0.171,0.120,0.150,0.210,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
18050,1979,СР,0.522,0.533,0.274,2.373,3.058,0.978,0.654,0.680,0.365,0.241,0.364,0.302,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
18050,1979,НГ,3.100,1.733,0.562,14.250,10.820,3.351,1.530,2.100,1.366,1.066,1.170,0.700,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
18050,1979,,,,НМ,0.090,СР,0.863,НГ,14.250,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
БГ表示最大值
CP表示平均值
NM表示最小值我认为您必须使用类似的方法从文件的每一行中获取第一个单词
string pathToFile = "Your path here";
char delimiter = ' ';// You have to use ',' if you need to split with ,
List<string> firstWordList = File.ReadAllLines(pathToFile).Select(x => x.Split(delimiter).First()).ToList();
在哪里,
File.ReadAllLinespathToFile会将文件中的行作为集合提供给您
.Selectx=>x.Splitdelimiter将帮助您遍历集合并根据拆分结果选择项
x、 Splitdelimiter。首先,Split将帮助您根据分隔符拆分列表中的每个项目,第一个将帮助您在拆分的值中取第一个。最后,变量firstWordList将包含所需的单词列表
我想你要比较两个文件。每行的第一个单词是发件人的名称。每个文件应该只有一个名称,并且每行应该以相同的名称开头 您读取了文件的行,但始终将找到的第一个名称与自身进行比较。您忘记使用当前行的名称 试试这个
using (OpenFileDialog dialog = new OpenFileDialog())
{
if (dialog.ShowDialog(this) == DialogResult.OK)
{
string sFileName = dialog.FileName;
pathFolder2 = sFileName;
label3.Text = pathFolder2;
label3.Show();
string[] lines = System.IO.File.ReadAllLines(dialog.FileName);
int i = 0;
foreach (var line in lines)
{
var splittedValues = line.Split(',');
var firstWord = splittedValues[0];
if (i == 0)
{
resultStation2 = firstWord;
}
else
{
if (resultStation2 != firstWord)
{
MessageBox.Show("Файла трябва да съдържа само една станция!");
return;
}
}
i++;
}
}
}
我更改了行结果。AddsplittedValues[0];to var firstWord=splittedValues[0];并使用变量firstWord而不是result[0]
更新
如果您想要文件中每行的所有年份,则必须使用年份列表
变量声明:
您应该在表单的构造函数中初始化列表
List<string> resultYears; //string resultYear;
List<string> resultYears2; //string resultYear2;
比较文件:
if (resultStation != resultStation2)
{
MessageBox.Show("Номера на станцията в единия файл не отговаря на номера на станцията в другият файл!" + Environment.NewLine + Environment.NewLine +
"ЗАБЕЛЕЖКА!" + Environment.NewLine + Environment.NewLine + "В двата файла, номера на станцията трябва да бъде един и същ!");
}
comboBox1.Items.Add(resultStation);
for (int i = 0; i < (this.resultYears.Count > this.resultYears2.Count ? this.resultYears.Count : this.resultYears2.Count); i++)
{
var year = i < this.resultYears.Count ? this.resultYears[i] : string.Empty;
var year2 = i < this.resultYears2.Count ? this.resultYears2[i] : string.Empty;
if (year == year2)
{
comboBox2.Items.Add(year);
}
else
{
MessageBox.Show("Годините от двата файла не съвпадат.");
}
}
创建第二个类:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DemoProject
{
public class DataFile
{
public DataFile()
{
this.Entries = new List<DataEntry>();
}
public List<DataEntry> Entries
{
get;
}
public static DataFile Load(string fileName)
{
DataFile result = new DataFile();
if(System.IO.File.Exists(fileName))
{
string[] allLines = System.IO.File.ReadAllLines(fileName);
foreach (var line in allLines)
{
var splittedValues = line.Split(',');
DataEntry entry = new DataEntry();
entry.Station = splittedValues[0];
entry.Year = splittedValues[1];
entry.Number = splittedValues[2];
entry.January = splittedValues[3];
entry.February = splittedValues[4];
entry.March = splittedValues[5];
entry.April = splittedValues[6];
entry.Mai = splittedValues[7];
entry.Juni = splittedValues[8];
entry.Juli = splittedValues[9];
entry.August = splittedValues[10];
entry.September = splittedValues[11];
entry.October = splittedValues[12];
entry.November = splittedValues[13];
entry.December = splittedValues[14];
}
}
return result;
}
/// <summary>
/// Returns true if all entries (all lines of the csv file) have the same station (the same first word).
/// If one entry have another first word it returns false.
/// </summary>
/// <returns></returns>
public bool CheckStation()
{
string station = string.Empty;
bool firstEntry = true;
foreach (var entry in this.Entries)
{
if(firstEntry)
{
station = entry.Station;
}
else
{
if(entry.Station != station)
{
return false;
}
}
}
return true;
}
/// <summary>
/// Returns the entries of the file which have the same year as given.
/// </summary>
/// <param name="year"></param>
/// <returns></returns>
public DataEntry[] GetAllEntriesOfOneYear(string year)
{
return this.Entries.Where(p => p.Year == year).ToArray();
}
/// <summary>
/// Returns true if this entries contains the same years as the entries of the given file
/// If one file have one more or less year it returns false
/// </summary>
/// <param name="otherFile"></param>
/// <returns></returns>
public bool CheckYears(DataFile otherFile)
{
string[] allPossibleYearsOfThisFile = this.GetYears();
string[] allPossibleYearsOfTheOtherFile = otherFile.GetYears();
if(allPossibleYearsOfThisFile.Length != allPossibleYearsOfTheOtherFile.Length)
{
return false;
}
else
{
foreach (var year in allPossibleYearsOfThisFile)
{
if(!allPossibleYearsOfTheOtherFile.Contains(year))
{
return false;
}
}
}
return true;
}
/// <summary>
/// Returns all possible years of the entries.
/// </summary>
/// <returns></returns>
public string[] GetYears()
{
return this.Entries.Select(p => p.Year).Distinct().ToArray();
}
}
}
更新
我发现了你的问题。数据文件类的加载方法不正确。
在对象上创建,然后在所有线上循环。始终更改这一对象的值,但不为每条线创建自己的对象。它只显示一年,因为它只有一年
修正荷载法如下所示:
public static DataFile Load(string[] allLines)
{
DataFile result = new DataFile();
foreach (var line in allLines)
{
DataEntry entry = new DataEntry();
var splittedValues = line.Split(',');
entry.Station = splittedValues[0];
entry.Year = splittedValues[1];
entry.Number = splittedValues[2];
entry.January = splittedValues[3];
entry.February = splittedValues[4];
entry.March = splittedValues[5];
entry.April = splittedValues[6];
entry.Mai = splittedValues[7];
entry.Juni = splittedValues[8];
entry.Juli = splittedValues[9];
entry.August = splittedValues[10];
entry.September = splittedValues[11];
entry.October = splittedValues[12];
entry.November = splittedValues[13];
entry.December = splittedValues[14];
result.Entries.Add(entry);
}
return result;
}
向我们展示您的代码以及失败的地方。通过使用StreamReader查看,您可以逐行读取文件。使用String.Split可以实现简单的分离,从第一个元素开始。我,更新我的post即使使用代码,也不清楚它不起作用是什么意思。到底是什么不起作用?如果再次使用变量,会出现此异常。请在你的问题中更新你的代码。我的代码已经更新了:你能告诉我如何使用object的实例吗:我不知道你是否初始化了构造函数中的两个列表,但我认为你做到了。现在有什么问题?如何为我的两个列表创建对象?this.resultYears=新列表;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DemoProject
{
public class DataFile
{
public DataFile()
{
this.Entries = new List<DataEntry>();
}
public List<DataEntry> Entries
{
get;
}
public static DataFile Load(string fileName)
{
DataFile result = new DataFile();
if(System.IO.File.Exists(fileName))
{
string[] allLines = System.IO.File.ReadAllLines(fileName);
foreach (var line in allLines)
{
var splittedValues = line.Split(',');
DataEntry entry = new DataEntry();
entry.Station = splittedValues[0];
entry.Year = splittedValues[1];
entry.Number = splittedValues[2];
entry.January = splittedValues[3];
entry.February = splittedValues[4];
entry.March = splittedValues[5];
entry.April = splittedValues[6];
entry.Mai = splittedValues[7];
entry.Juni = splittedValues[8];
entry.Juli = splittedValues[9];
entry.August = splittedValues[10];
entry.September = splittedValues[11];
entry.October = splittedValues[12];
entry.November = splittedValues[13];
entry.December = splittedValues[14];
}
}
return result;
}
/// <summary>
/// Returns true if all entries (all lines of the csv file) have the same station (the same first word).
/// If one entry have another first word it returns false.
/// </summary>
/// <returns></returns>
public bool CheckStation()
{
string station = string.Empty;
bool firstEntry = true;
foreach (var entry in this.Entries)
{
if(firstEntry)
{
station = entry.Station;
}
else
{
if(entry.Station != station)
{
return false;
}
}
}
return true;
}
/// <summary>
/// Returns the entries of the file which have the same year as given.
/// </summary>
/// <param name="year"></param>
/// <returns></returns>
public DataEntry[] GetAllEntriesOfOneYear(string year)
{
return this.Entries.Where(p => p.Year == year).ToArray();
}
/// <summary>
/// Returns true if this entries contains the same years as the entries of the given file
/// If one file have one more or less year it returns false
/// </summary>
/// <param name="otherFile"></param>
/// <returns></returns>
public bool CheckYears(DataFile otherFile)
{
string[] allPossibleYearsOfThisFile = this.GetYears();
string[] allPossibleYearsOfTheOtherFile = otherFile.GetYears();
if(allPossibleYearsOfThisFile.Length != allPossibleYearsOfTheOtherFile.Length)
{
return false;
}
else
{
foreach (var year in allPossibleYearsOfThisFile)
{
if(!allPossibleYearsOfTheOtherFile.Contains(year))
{
return false;
}
}
}
return true;
}
/// <summary>
/// Returns all possible years of the entries.
/// </summary>
/// <returns></returns>
public string[] GetYears()
{
return this.Entries.Select(p => p.Year).Distinct().ToArray();
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace DemoProject
{
public partial class Form1 : Form
{
DataFile firstFile;
DataFile secondFile;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
using (OpenFileDialog dialog = new OpenFileDialog())
{
if (dialog.ShowDialog(this) == DialogResult.OK)
{
string sFileName = dialog.FileName;
pathFolder = sFileName;
label3.Text = pathFolder;
label3.Show();
firstFile = DataFile.Load(dialog.FileName);
if(firstFile.CheckStation())
{
MessageBox.Show("Файла с дневни данни трябва да съдържа само една станция!");
}
}
}
}
private void button2_Click(object sender, EventArgs e)
{
using (OpenFileDialog dialog = new OpenFileDialog())
{
if (dialog.ShowDialog(this) == DialogResult.OK)
{
string sFileName = dialog.FileName;
pathFolder2 = sFileName;
label4.Text = pathFolder2;
label4.Show();
string[] lines = System.IO.File.ReadAllLines(dialog.FileName);
int i = 0;
secondFile = DataFile.Load(dialog.FileName);
if (secondFile.CheckStation())
{
MessageBox.Show("Файла с месечни данни трябва съдържа само една станция!");
return;
}
}
}
}
private void button3_Click(object sender, EventArgs e)
{
if (firstFile != null && secondFile != null)
{
if (firstFile.Entries.First().Station != secondFile.Entries.First().Station)
{
MessageBox.Show("Номера на станцията в единия файл не отговаря на номера на станцията в другият файл!" + Environment.NewLine + Environment.NewLine +
"ЗАБЕЛЕЖКА!" + Environment.NewLine + Environment.NewLine + "В двата файла, номера на станцията трябва да бъде един и същ!");
}
comboBox1.Items.Add(firstFile.Entries.First().Station);
if (firstFile.CheckYears(secondFile))
{
comboBox2.Items.AddRange(firstFile.GetYears());
}
else
{
MessageBox.Show("Годините от двата файла не съвпадат.");
}
}
else
{
MessageBox.Show("One or both files are empty. Please select the file and read the data first.");
}
}
public void loadEntries()
{
string selectedYear = this.comboBox2.SelectedValue.ToString();
DataEntry[] entries = firstFile.GetAllEntriesOfOneYear(selectedYear);
//TODO:
//Now you have all lines of the first file which have the selected year.
//With this list you can work
}
}
}
public static DataFile Load(string[] allLines)
{
DataFile result = new DataFile();
foreach (var line in allLines)
{
DataEntry entry = new DataEntry();
var splittedValues = line.Split(',');
entry.Station = splittedValues[0];
entry.Year = splittedValues[1];
entry.Number = splittedValues[2];
entry.January = splittedValues[3];
entry.February = splittedValues[4];
entry.March = splittedValues[5];
entry.April = splittedValues[6];
entry.Mai = splittedValues[7];
entry.Juni = splittedValues[8];
entry.Juli = splittedValues[9];
entry.August = splittedValues[10];
entry.September = splittedValues[11];
entry.October = splittedValues[12];
entry.November = splittedValues[13];
entry.December = splittedValues[14];
result.Entries.Add(entry);
}
return result;
}