teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

3

追記

2019/10/31 04:44

投稿

inari_ken
inari_ken

スコア34

title CHANGED
File without changes
body CHANGED
@@ -77,4 +77,111 @@
77
77
  Version 15.7.3
78
78
  VisualStudio.15.Release/15.7.3+27703.2026
79
79
  Microsoft .NET Framework
80
- Version 4.7.03190
80
+ Version 4.7.03190
81
+
82
+ ### サンプルプログラム(2019年10月31日13時43分追記)
83
+ 再現しないとご指摘があったため、List生成から呼び出しまでのサンプルプログラムを作成しました。
84
+ 渡しの環境では以下プログラムで同じエラーが発生します。
85
+ ```C#
86
+ using System.Collections.Generic;
87
+ using System.Linq;
88
+
89
+ namespace ConsoleApp3
90
+ {
91
+ class Program
92
+ {
93
+ static void Main(string[] args)
94
+ {
95
+
96
+ List<string[]> data1 = new List<string[]>();
97
+ List<string[]> data2 = new List<string[]>();
98
+
99
+ List<dynamic> agg1 = new List<dynamic>();
100
+ List<dynamic> agg2 = new List<dynamic>();
101
+
102
+ data1.Add(new string[4] { "1", "2", "3", "4" });
103
+ data1.Add(new string[4] { "2", "2", "3", "4" });
104
+ data1.Add(new string[4] { "3", "2", "3", "4" });
105
+
106
+ data2.Add(new string[4] { "1", "2", "3", "4" });
107
+ data2.Add(new string[4] { "2", "2", "3", "4" });
108
+ data2.Add(new string[4] { "3", "2", "3", "4" });
109
+ data2.Add(new string[4] { "aaa", "2", "3", "4" });
110
+
111
+ int item_count = 0;
112
+ for (int j = 0; j < 2; j++)
113
+ {
114
+
115
+ agg1.Add(new
116
+ {
117
+ title = j.ToString(),
118
+ result = data1.GroupBy(x => x[item_count])
119
+ .Select(x => new { Choise = x.Key, Count = x.Count() })
120
+ .OrderBy(x => x.Choise).ToList()
121
+ });
122
+
123
+ item_count++;
124
+
125
+ }
126
+
127
+ item_count = 0;
128
+ for (int j = 0; j < 2; j++)
129
+ {
130
+
131
+ agg2.Add(new
132
+ {
133
+ title = j.ToString(),
134
+ result = data2.GroupBy(x => x[item_count])
135
+ .Select(x => new { Choise = x.Key, Count = x.Count() })
136
+ .OrderBy(x => x.Choise).ToList()
137
+ });
138
+
139
+ item_count++;
140
+
141
+ }
142
+
143
+
144
+ agg1 = List_Comp(agg1, agg2);
145
+
146
+
147
+
148
+
149
+ }
150
+ private static List<dynamic> List_Comp(List<dynamic> target, List<dynamic> reference)
151
+ {
152
+
153
+ int list_count = 0;
154
+ bool Contains_flg = false;
155
+ foreach (dynamic dy in reference)
156
+ {
157
+ foreach (var item in dy.result)
158
+ {
159
+ Contains_flg = false;
160
+ foreach (var it in target[list_count].result)
161
+ {
162
+ if (item.Choise == it.Choise)
163
+ {
164
+ Contains_flg = true;
165
+ }
166
+ }
167
+ if (Contains_flg == false)
168
+ {
169
+ // 選択肢の追加
170
+ // 実行時エラー
171
+ target[list_count].result.Add(new { Choise = item.Choise, Count = 0 });
172
+ }
173
+ }
174
+ list_count++;
175
+ }
176
+
177
+ return (target);
178
+ }
179
+
180
+
181
+
182
+ }
183
+
184
+
185
+ }
186
+
187
+ ```

2

コメントの修正

2019/10/31 04:44

投稿

inari_ken
inari_ken

スコア34

title CHANGED
File without changes
body CHANGED
@@ -39,12 +39,12 @@
39
39
  if(Contains_flg == false)
40
40
  {
41
41
  // 選択肢の追加
42
- // 実行時エラー
42
+ // 実行時エラー
43
43
  target[list_count].result.Add(new { Choise = item.Choise.ToString(), Count = 0 });
44
44
 
45
- // 試したこと① 正常にListのresultにAddされた
45
+ // 試したこと① 正常にListのresultにAddされた
46
46
  // target[list_count].result.Add(new { Choise = "aaa", Count = 0 });
47
- // 試したこと② 正常にメッセージ"aaa"が表示された
47
+ // 試したこと② 正常にメッセージ"aaa"が表示された
48
48
  // MessageBox.Show(item.Choise);
49
49
  }
50
50
  }

1

コードの修正

2019/10/31 02:54

投稿

inari_ken
inari_ken

スコア34

title CHANGED
File without changes
body CHANGED
@@ -38,10 +38,14 @@
38
38
  }
39
39
  if(Contains_flg == false)
40
40
  {
41
- // 選択肢の追加
41
+ // 選択肢の追加
42
+ // ←実行時エラー
42
- target[list_count].result.Add(new { Choise = item.Choise.ToString(), Count = 0 }); // ←実行時エラー
43
+ target[list_count].result.Add(new { Choise = item.Choise.ToString(), Count = 0 });
44
+
45
+ // ←試したこと① 正常にListのresultにAddされた
43
- // target[list_count].result.Add(new { Choise = "aaa", Count = 0 }); // ←試したこと① 正常にListのresultにAddされた
46
+ // target[list_count].result.Add(new { Choise = "aaa", Count = 0 });
44
- // MessageBox.Show(item.Choise); // ←試したこと② 正常にメッセージ"aaa"が表示された
47
+ // ←試したこと② 正常にメッセージ"aaa"が表示された
48
+ // MessageBox.Show(item.Choise);
45
49
  }
46
50
  }
47
51
  list_count++;