2016-09-14 5 views
0

にリストを変換:私はこのフォームでのデータのリストを持っている2次元アレイ

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

そして、私は2次元配列をしたい、それはすべての文字列である必要があり、変数リストのサイズを無視することができます。配列は次のようになります。

私はこのように試しました。私はちょっと固まっています。簡単な方法で

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'内のすべてのエントリは1のカウントを持っていますか? – 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日」]例えば、あなたはまだ2次元配列をしたい場合は、 dの辞書を変換するいくつかの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(); 
    } 
+0

コードを少し変更しました。ここで、配列が追加された数を代入します。[group、date] = "0"はエントリが存在しない場合です。今私はatleastデータ配列に同じ日付の2行を追加する必要があります、それ以外の場合は、まったく表示されません。 – CSnacker

+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); 
} 
関連する問題