将列表转换为2D数组

时间:2016-09-14 07:13:14

标签: c# arrays list

我有一个这种形式的数据列表:

group     date        count
L1        2016-09-13  1
L2        2016-09-13  2
L3        2016-09-13  3
L1        2016-09-12  1
L2        2016-09-12  2
L3        2016-09-12  3
...       ...         ...

我想要一个2D数组,它应该是所有字符串,让我们忽略变量列表大小。数组应如下所示:

group    2016-09-13   2016-09-12
L1       1            1
L2       2            2
L3       3            3

我试过这种方式,我有点卡住了。

public class ChartTmp
{
    public string date, group, count;
}
List<ChartTmp> list = new List<ChartTmp>();
//... fill list with data
string [,] data = new string[15, 15];

    data[0, 0] = "group";
    for (int i = 0; i<15;i++ ){
        curr_date = list[i].date;
        if (last_date != curr_date) {
            data[0, counter] = curr_date;
            last_date = curr_date;
            counter++;
        }
        data[0, counter] = list[i].group;
        data[i, counter] = list[i].count;
    }

2 个答案:

答案 0 :(得分:0)

以简单的方式,您应该首先将标题(日期)和组分开

List<ChartTmp> list = new List<ChartTmp>();
ChartTmp t = new ChartTmp();
t.group = "L1";
t.date = "2016-09-13";
t.count = "1";
list.Add(t);
t = new ChartTmp();
t.group = "L2";
t.date = "2016-09-13";
t.count = "2";
list.Add(t);
t = new ChartTmp();
t.group = "L3";
t.date = "2016-09-13";
t.count = "3";
list.Add(t);
t = new ChartTmp();
t.group = "L1";
t.date = "2016-09-12";
t.count = "4";
list.Add(t);
t = new ChartTmp();
t.group = "L2";
t.date = "2016-09-12";
t.count = "5";
list.Add(t);
t = new ChartTmp();
t.group = "L3";
t.date = "2016-09-12";
t.count = "6";
list.Add(t);

// get the header and group
List<string> headers = list.Select(ct => ct.date).Distinct().OrderBy(s => s).ToList();
List<string> groups = list.Select(ct => ct.group).Distinct().OrderBy(s => s).ToList();

string[,] data = new string[15, 15];

// create header
data[0, 0] = "group";
for (var i = 0; i < headers.Count(); i++)
{
    data[0, i + 1] = headers[i];
}

// create content
for (var i = 0; i < groups.Count(); i++)
{
    data[i + 1, 0] = groups[i];

    for (var j = 0; j < headers.Count(); j++)
    {
        data[i + 1, j + 1] = list.Where(ct => ct.group == groups[i] && ct.date == headers[j]).Select(ct => ct.count).FirstOrDefault();
    }
}

// print the test result
int rowLength = data.GetLength(0);
int colLength = data.GetLength(1);

for (int i = 0; i < rowLength; i++)
{
    for (int j = 0; j < colLength; j++)
    {
        Console.Write(string.Format("{0} ", data[i, j]));
    }
    Console.Write(Environment.NewLine + Environment.NewLine);
}

答案 1 :(得分:0)

您可以使用嵌套字典,因此无需太多代码即可轻松聚合数据,例如

    static void Main(string[] args)
    {
        var data = new[]{
            new { group="L1", date="2016-09-13", count=1},
            new { group="L2", date="2016-09-13", count=2},
            new { group="L3", date="2016-09-13", count=3},
            new { group="L1", date="2016-09-12", count=1},
            new { group="L2", date="2016-09-12", count=2},
            new { group="L3", date="2016-09-12", count=3}
        };

        //convert data to dictionaries
        var dictionaries = new Dictionary<string, Dictionary<string, int>>();
        foreach (var row in data)
        {
            if (!dictionaries.ContainsKey(row.group))
                dictionaries[row.group] = new Dictionary<string, int>();
            if (!dictionaries[row.group].ContainsKey(row.date))
                dictionaries[row.group][row.date] = row.count;
        }

我认为它已经非常容易使用和显示,你可以使用词典[“L1”] [“2016-09-13”]获取计数,例如,如果你仍然想要2D数组,转换字典词典使用一些for循环的2D数组

        //convert dictionary of dictionaries to 2D array
        int groupNum = dictionaries.Keys.Count, dateNum = dictionaries.First().Value.Keys.Count;
        string[,] array = new string[groupNum + 1, dateNum + 1];
        array[0, 0] = "group";

        //assign dates
        for (int i = 1; i <= dateNum; i++)
            array[0, i] = dictionaries.First().Value.Keys.ElementAt(i - 1);

        //assign groups
        for (int i = 1; i <= groupNum; i++)
            array[i, 0] = dictionaries.Keys.ElementAt(i - 1);

        //assign counts
        for (int group = 1; group <= groupNum; group++)
            for (int date = 1; date <= dateNum; date++)
            {
                array[group, date] = "0";
                string groupName = array[group,0], dateString = array[0,date];
                if(dictionaries[groupName].ContainsKey(dateString))
                    array[group, date] = dictionaries[groupName][dateString].ToString();
            }

        //print the 2D array
        for (int row = 0; row < groupNum + 1; row++)
        {
            for (int column = 0; column < dateNum + 1; column++)
                Console.Write("{0} ", array[row, column]);
            Console.WriteLine();
        }

        Console.ReadLine();
    }
相关问题