-1

我有一个数据集,我通过以下方式输出到列表中:

using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using System.Linq;


using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;

class Program
{
    static void Main(string[] args)
    {
        List<object> myList = new List<object>();

    int i = 1;
    using (StreamReader reader = new StreamReader("file.dat"))
    {
        string line;
        var locations = new Dictionary<string, int[]>() {
            {"210", new [] {405, 4, 128, 12, 141, 12, 247, 15, 121, 3}}, 
            {"310", new [] {321, 4, 112, 12, 125, 12, 230, 15, 105, 3}}, 
            {"410", new [] {477, 4, 112, 12, 125, 12, 360, 15, 105, 3}} 
        };

        while ((line = reader.ReadLine()) != null)
        {


            var lineStart = line.Substring(0, 3);

            if (lineStart == "210" || lineStart == "310" || lineStart == "410")
            {
                var currentLocations = locations[lineStart];
                var letters = line.Substring(currentLocations[0], currentLocations[1]);

                var tvolume =
                    int.Parse(line.Substring(currentLocations[2], currentLocations[3])) +
                    int.Parse(line.Substring(currentLocations[4], currentLocations[5]));

                var tprice = long.Parse(line.Substring(currentLocations[6], currentLocations[7]));
                var mvolume = tprice * tvolume * 0.01 * 0.0000001;
                var currency = line.Substring(currentLocations[8], currentLocations[9]);

                myList.Add(i);
                myList.Add(lineStart);
                myList.Add(letters);
                myList.Add(tvolume);
                myList.Add(tprice);
                myList.Add(mvolume);
                myList.Add(currency);

                // double total = myList.

                Console.WriteLine(i);
                Console.WriteLine(lineStart);
                Console.WriteLine(letters);
                Console.WriteLine(tvolume * 0.01);
                Console.WriteLine(tprice * 0.0000001);
                Console.WriteLine("{0:N}", mvolume);
                Console.WriteLine(currency + "\n");
                i = i + 1;
            }

        }
        int index = 0;
        Dictionary<string, int> tvolumeDictionary = new Dictionary<string, int>();
        Dictionary<string, long> mvolumeDictionary = new Dictionary<string, long>();
        string listLetters;

        foreach (object item in myList)
        {
            switch (index % 7)
            {
                case 2:
                    listLetters = item.ToString();
                    if (!tvolumeDictionary.Keys.Contains(listLetters))
                    {
                        tvolumeDictionary.Add(listLetters, 0);
                        mvolumeDictionary.Add(listLetters, 0);
                    }
                    break;
                case 3:
                    listLetters = myList[index - 1].ToString();
                    tvolumeDictionary[listLetters] = tvolumeDictionary[listLetters] + (int)item;
                    break;
                case 5:
                    listLetters = myList[index - 3].ToString();
                    mvolumeDictionary[listLetters] = mvolumeDictionary[listLetters] + long.Parse(item.ToString());
                    break;
            }
            index++;
        }

                //   Console.WriteLine(myList.Count);

        foreach (KeyValuePair<string, int> entry in tvolumeDictionary)
        {
            Console.WriteLine("{0} tvolume: {1} mVolume: {2}", entry.Key, entry.Value, mvolumeDictionary[entry.Key]);
        }

    }
}

}

File.dat 有数千行,其中将返回数百行。我希望为每个“字母”提供数字总数的细分(字母中只有大约 4 个类别)。AAAA、BBBB、CCCC 和 DDDD。如果这是 excel,我希望它对字母列中的每个更改执行小计功能,但这里的问题是它们不按顺序排列。

所需的输出是

 AAAA: TOTAL TVOLUME: TOTAL MVOLUME 
 BBBB: TOTAL TVOLUME: TOTAL MVOLUME
 CCCC: TOTAL TVOLUME: TOTAL MVOLUME
 DDDD: TOTAL TVOLUME: TOTAL MVOLUME

上述格式不是必需的)。

4

3 回答 3

1

[编辑] 上一个答案已被版主删除,我无法取消删除,所以我在另一个答案下发布。在处理提取的数据时,您似乎希望拥有更大的灵活性,我强烈建议您使用自定义类和 Linq,而不是包含自定义类对象的列表:

using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using System.Linq;

class Program
{
    public class EntryLine
    {
        public int I { get; set; }
        public string LineStart { get; set; }
        public string Letters { get; set; }
        public int TVolume { get; set; }
        public long TPrice { get; set; }
        public double MVolume { get; set; }
        public string Currency { get; set; }

    }

    static void Main(string[] args)
    {
        List<EntryLine> myList = new List<EntryLine>();
        int i = 1;
        using (StreamReader reader = new StreamReader("file.dat"))
        {
            string line;
            var locations = new Dictionary<string, int[]>() {
            {"210", new [] {405, 4, 128, 12, 141, 12, 247, 15, 121, 3}}, 
            {"310", new [] {321, 4, 112, 12, 125, 12, 230, 15, 105, 3}}, 
            {"410", new [] {477, 4, 112, 12, 125, 12, 360, 15, 105, 3}} 
        };

            while ((line = reader.ReadLine()) != null)
            {
                var lineStart = line.Substring(0, 3);

                if (lineStart == "210" || lineStart == "310" || lineStart == "410")
                {
                    var currentLocations = locations[lineStart];
                    var letters = line.Substring(currentLocations[0], currentLocations[1]);

                    var tvolume =
                        int.Parse(line.Substring(currentLocations[2], currentLocations[3])) +
                        int.Parse(line.Substring(currentLocations[4], currentLocations[5]));

                    var tprice = long.Parse(line.Substring(currentLocations[6], currentLocations[7]));
                    var mvolume = tprice * tvolume * 0.01 * 0.0000001;
                    var currency = line.Substring(currentLocations[8], currentLocations[9]);

                    myList.Add(new EntryLine()
                    {
                        I = i,
                        LineStart = lineStart,
                        Letters = letters,
                        TVolume = tvolume,
                        TPrice = tprice,
                        MVolume = mvolume,
                        Currency = currency
                    });
                    i = i + 1;
                }
            }

            var x = myList.GroupBy(g => new { g.Letters, g.Currency })
                .Select(a => new { a.Key.Letters, a.Key.Currency, TSum = a.Sum(s => s.TVolume), MSum = a.Sum(s => s.MVolume) });

            foreach (var item in x)
            {
                Console.WriteLine("{0} currency: {1} tvolume: {2} mVolume: {3}", item.Letters, item.Currency, item.TSum, item.MSum);
            }
        }
    }
}
于 2012-12-03T15:02:08.920 回答
0

你需要打破你的显示循环。

尝试添加

If (i > 3) break; // Exit while loop

i计数器递增后的行(i= i+1;行)。

于 2012-12-03T11:23:59.540 回答
0

您可以使用 aDictionary<string, long>来跟踪总数。您可以添加值为 0 的所有已知键,也可以在添加值之前检查字典是否包含值。像这样的东西:

Dictionary<string, long> values;
long value = code to get the value;
string key = code to get the key;
if(values.ContainsKey(key))
    values[key] += value;
else
    values[key] = value;
于 2012-12-03T11:34:10.340 回答