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);
    }
4

3 回答 3

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();
于 2012-12-11T18:35:09.653 回答
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);
于 2012-12-11T22:02:23.180 回答
-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);
}
于 2012-12-11T18:40:43.397 回答