计算和删除数字

时间:2013-10-16 09:04:04

标签: c#

我已从一个文件中读取匹配并向另一个文件写入一个数字匹配的次数。 如何删除每个号码,只留下这个号码的一条记录。我的意思是现在我得到:

Count of matching numbers in this group: 123
Numbers:
...123 times this number i.e 123 lines.

如何删除此号码并按以下方式编写:

Count of matching numbers in this group: 123
Number: the number

这就是我现在这样做的方式:

        string path = "file1.txt";

        var result = (File.ReadLines(path)).OrderBy(x => x).GroupBy(x => x);

        using (var writer = new StreamWriter("file2.txt"))
        {
            foreach (var item in result)
            {
                string outGrp = String.Format("Count of matching numbers in this group: {0}", item.Count());
                Console.WriteLine(outGrp);
                writer.WriteLine(outGrp, true);
                Console.WriteLine("Numbers:");
                writer.WriteLine("Numbers:", true);

                foreach (var member in item)
                {
                    Console.WriteLine(member);
                    writer.WriteLine(member, true);
                }
            }
        }

2 个答案:

答案 0 :(得分:3)

我想你要改变

foreach (var member in item)
{
    Console.WriteLine(member);
    writer.WriteLine(member, true);
}

这样的事情:

var number = item.First();

Console.WriteLine(number);
writer.WriteLine(number, true);

答案 1 :(得分:1)

一个班轮

foreach (var member in item)
{
    Console.WriteLine(member);
    writer.WriteLine(member, true);
    break; //<---- slam on the brake (sp?) D:
}