2016-09-14 94 views
0

我有數據,這種形式的列表:轉換列表到二維數組

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 
...  ...   ... 

而且我希望有一個二維數組,它應該是所有的字符串,可以忽略的變量列表的大小。該陣列應該是這樣的:

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

什麼是錯誤,或不是預期的? – user5226582

+0

這些組的「count」屬性是否相同?那麼'L1'中的所有條目都有一個數? – HimBromBeere

+0

還有什麼'cd'和'list'在哪裏? –

回答

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年9月13日」例如,如果你還想二維數組,轉換d的字典字典到二維數組使用一些for循環

 //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(); 
    } 
+0

我已經改變了一點點的代碼,如果條目不存在,你在那裏分配了我添加數組[數組,日期] =「0」的計數。現在,我必須至少在數據數組中添加2行具有相同日期的數據,否則他不會顯示它。 – CSnacker

+0

啊,是的,代碼假設源有足夠的數據。好主意,最好是安全而不是遺憾。 – 2016-09-14 14:45:43

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