2012-12-11 27 views
1

我有幾個標準,如只挑選記錄到一天,也如果一條記錄包含字母'CA', 現在我想添加一個條件之前這兩個我跳過任何記錄是重複基於身份證號碼。如果ID號重複,如何跳過文本文件中的行?

這裏是我的代碼

private void btn_convert_Click(object sender, EventArgs e) 
    { 
     if ((textBox1.Text == "")&& (textBox2.Text == "")) 
     { 
      MessageBox.Show("Please specify input and output"); 
     } 

     StringBuilder csvFile = new StringBuilder(); 
     string temp = "";  
     // string[] file = File.ReadAllLines(@"C:\Users\Program\Desktop\test.txt");   
     string[] file = File.ReadAllLines(textBox1.Text); 
     foreach (string line in file) 
     { 
      //here I want to add an if condition where only write the unique records based on ID number 

      if ((line.Contains(DateTime.Now.ToString("MM/dd/yyyy"))) && (line.Contains("\tAU\t"))){ 
      if (line.Contains("\t")) 
       { 
       temp = line.Replace("\t", ","); 

       csvFile.Append(temp + "\r\n"); 

       continue; 
       } 
      csvFile.Append(line + "\r\n"); 
       } 
     } 

    //File.WriteAllText(@"C:\Users\Program\Desktop\test.csv", csvFile.ToString()); 
     File.WriteAllText((textBox2.Text + "\\test.csv"), csvFile.ToString()); 
     MessageBox.Show("CSV file successfully created at the following location :\n" + textBox2.Text); 
    } 
+2

不知道爲什麼人們正在縮小過於本地化,您可以忽略代碼和完全的輸入/輸出和問題仍然是交代,並適用於其他人同樣的情況(這是一種常見的情況,所以會有其他人)。 – Servy

回答

1

從你的榜樣,我想你想完全排除記錄中出現多次,而不是隻保留第一次出現?

file = file.Where(line => line.Contains("\t")) 
      .GroupBy(line => line.Substring(0, line.IndexOf('\t'))) 
      .Where(g => g.Count() == 1) 
      .Select(g => g.First()) 
      .ToArray(); 
+0

你不想刪除所有重複的記錄,只是當它們重複時刪除所有的行,實際上是一個'Distinct'(你需要使用'DistinctBy'或自定義比較器,因爲他沒有比較每個屬性格式)。 – Servy

+0

OP的例子似乎要求相反。但第二點你是對的。我只需要比較第一部分。 – Douglas

+1

你是對的。鑑於此,您需要將'GroupBy'調整爲'line => getIDFromLine(line)'也需要將select改爲'g.Single' – Servy

-1

這只是快速和骯髒的,我不能在此刻進行測試,所以我甚至無法保證它會工作作爲 - 是的,但它應該是足夠接近你展示的方式你能行的。

private void btn_convert_Click(object sender, EventArgs e) 
{ 
    if ((textBox1.Text == "") && (textBox2.Text == "")) 
    { 
     MessageBox.Show("Please specify input and output"); 
    } 

    StringBuilder csvFile = new StringBuilder(); 
    string temp = ""; 
    string[] file = File.ReadAllLines(textBox1.Text); 
    List<int> idList = new List<int>(); 
    foreach (string line in file) 
    { 
     string[] cols = line.Split('\t'); 
     if (cols.Length > 0) 
     { 
      int colId = -1; 
      if (int.TryParse(cols[0], out colId)) 
      { 
       if (idList.contains(colId)) 
       { 
        continue; 
       } 
       idList.Add(colId); 
      } 
     } 

     if ((line.Contains(DateTime.Now.ToString("MM/dd/yyyy"))) && (line.Contains("\tAU\t"))) 
     { 
      if (line.Contains("\t")) 
      { 
       temp = line.Replace("\t", ","); 

       csvFile.Append(temp + "\r\n"); 

       continue; 
      } 
      csvFile.Append(line + "\r\n"); 
     } 
    } 

    File.WriteAllText((textBox2.Text + "\\test.csv"), csvFile.ToString()); 
    MessageBox.Show("CSV file successfully created at the following location :\n" + textBox2.Text); 
} 
0

嘗試這種方法

 string x = ""; 
     using (StreamReader sr = new StreamReader(@"d:\test.txt")) 
      x = sr.ReadToEnd(); 

     var mynewlist = (from v in x.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries) 
         let items = v.Split(new string[] { "\t" }, StringSplitOptions.RemoveEmptyEntries) 
         where items.Count() == 5 
         select new 
         { 
          ID = int.Parse(items[0]), 
          FamilyN = items[1], 
          LastN = items[2], 
          Country = items[3], 
          Date = DateTime.ParseExact(items[4], "MM/dd/yyyy", System.Globalization.CultureInfo.InvariantCulture) 
         }).GroupBy(X => X.ID).Where(X => X.Count() == 1).Select(X => X.First());//add any condition here 

     //mynewlist is a strongly typed List 
     string output = ""; 
     foreach (var item in mynewlist) 
      output += item.ID + "," + item.FamilyN + "," + item.LastN + "," + item.Country + "," + item.Date.ToString("MM/dd/yyyy") + "\r\n"; 


     using (StreamWriter sw = new StreamWriter(@"d:\test.csv")) 
      sw.Write(output); 
相關問題