2017-04-04 84 views
0

我必須讀取分佈不同的兩個文件。如果第一個元素與第二個元素相同,我還需要進行比較。沒有數字,只是一個文件中的電臺№與另一個文件中的№電臺相同(這兩個文件的第一個元素),那麼我需要比較同一年的開始和結束年份。 然後,您需要從第一個文件的開始到結束在station和combobox2年的combobox1№收費。 然後我需要從第一個文件中讀取所選年份的31行。如何讀取前31行並採取每行C#的第三個元素?

我不知道爲什麼combobox2只填滿一年而不是所有年份。

創建類的DataEntry:

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 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.Linq; 
using System.Text; 
using System.Threading.Tasks; 

namespace WindowsFormsApp1 
{ 
    public class Service 
    { 
     internal string[] GetAllYears(DataFile firstFile) 
     { 
     //  DataFile rstFile = firstFile; 

      return firstFile.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) 
     { 

     } 


    } 
} 

真的我不知道爲什麼combobox2沒有填滿所有年份,只有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, 

向下推移相同的方式與隨後的一年,直到2015年

二CSV文件:

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表示最小值

+1

向我們展示你的代碼,它失敗。 – Marco

+1

查看http://filehelpers.net –

+0

通過使用「StreamReader」,您可以逐行讀取文件。一個簡單的分離可以通過使用'String.Split()'來完成,你可以從第一個元素開始。 – Oliver

回答

1

我認爲你必須使用這樣的東西來獲取文件每一行的第一個單詞;

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.ReadAllLines(pathToFile)的意志給你在文件中以線條爲收集
  • .Select(x => x.Split(delimiter)會幫助你通過收集迭代,並選擇基礎上split結果項目
  • x.Split(delimiter).First(), Split將幫助您根據分隔符分割列表中的每個項目,並且first()將幫助您採取首先是分裂值。所以最後變量firstWordList將包含所需的單詞列表
0

我想你想比較兩個文件。每行中的第一個單詞是發件人的名稱。每個文件只能有一個名稱,每行應該以相同的名稱開頭。

您閱讀文件的行,但始終將自己的第一個找到的名稱進行比較。您忘記使用當前行的名稱。

嘗試此

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++; 
     } 
    } 
} 

我改變了線result.Add(splittedValues[0]);var firstWord = splittedValues[0];和所使用的可變firstWord代替result[0]

更新 如果你想要文件中每一行的所有年份,你必須使用年份列表。

變量聲明: (你應該初始化列表形式的構造函數)

List<string> resultYears; //string resultYear; 
List<string> resultYears2; //string resultYear2; 

讀取文件:

var year = splittedValues[1]; 

resultYears.Add(year); 

比較文件:

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("Годините от двата файла не съвпадат."); 
    } 
} 

更新2:

我幫了你的objectModel。

創建一個類的DataEntry:

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 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 
     } 
    } 
} 

更新

我發現你的問題。 DataFile類的加載方法不正確。 您在對象上創建,然後遍歷所有行。你總是改變這個對象的值,但不是爲每一行創建一個自己的對象。它只顯示一年,因爲它只有一年。

的修正後的裝載方法是

​​
+0

如果您第二次使用變量,則會得到此異常。請更新您問題中的代碼。 – Hank

+0

我的代碼已經更新:)你可以告訴我如何採取對象的實例:) –

+0

我不知道你是否初始化了構造函數中的兩個列表,但我認爲你做到了。現在有什麼問題? – Hank

相關問題