質問編集履歴
4
得たい辞書情報を追加(外からデータ構造にアクセスできるように、フィールド名を持たせた。)入出力条件を追加。
title
CHANGED
File without changes
|
body
CHANGED
@@ -35,8 +35,36 @@
|
|
35
35
|
}
|
36
36
|
```
|
37
37
|
|
38
|
-
###
|
38
|
+
### 得たい辞書情報
|
39
|
+
```Json
|
40
|
+
[
|
41
|
+
{
|
42
|
+
"Country": "JP",
|
43
|
+
"PersonalData": [
|
44
|
+
{
|
45
|
+
"Name": "Taro",
|
46
|
+
"Login": 11
|
47
|
+
},
|
48
|
+
{
|
49
|
+
"Name": "Jiro",
|
50
|
+
"Login": 5
|
51
|
+
}
|
52
|
+
]
|
53
|
+
},
|
54
|
+
{
|
55
|
+
"Country": "US",
|
56
|
+
"PersonalData": [
|
57
|
+
{
|
58
|
+
"Name": "Mike",
|
59
|
+
"Login": 20
|
60
|
+
}
|
61
|
+
]
|
62
|
+
}
|
63
|
+
]
|
64
|
+
```
|
39
65
|
|
66
|
+
### 該当のソースコード(元コード)
|
67
|
+
|
40
68
|
```C#
|
41
69
|
using System;
|
42
70
|
using System.Collections.Generic;
|
@@ -227,4 +255,8 @@
|
|
227
255
|
}
|
228
256
|
}
|
229
257
|
}
|
230
|
-
```
|
258
|
+
```
|
259
|
+
|
260
|
+
### さらに追記
|
261
|
+
* 辞書は外部にてJsonデータとして使えるようにフィールド名を持つこと。
|
262
|
+
* AnotherからIEnumerableで1件ずつ来る仕様は変えてはならない。
|
3
GetHumansの中でという表現だと、情報を持つ側で重複削除&加算したものを返すという誤解がありそうだったため、修正。
title
CHANGED
File without changes
|
body
CHANGED
@@ -9,7 +9,7 @@
|
|
9
9
|
* `GetHumansメソッド`: (出来ればプロパティにしたい)情報を順に取り出す。
|
10
10
|
*
|
11
11
|
* `Programクラス`
|
12
|
-
* GetHumansの中で、国名、名前、ログイン回数の多段辞書を作る。(Jsonとして外に出す情報)
|
12
|
+
* GetHumans~~の中で、~~から国名、名前、ログイン回数の多段辞書を作る。(Jsonとして外に出す情報)
|
13
13
|
** 国名、名前が等しい場合、ログイン回数は加算する。**
|
14
14
|
|
15
15
|
### 発生している問題
|
2
得られた回答から少し修正を加えたものを追記
title
CHANGED
File without changes
|
body
CHANGED
@@ -124,4 +124,107 @@
|
|
124
124
|
```
|
125
125
|
|
126
126
|
### 補足情報
|
127
|
-
現在試している環境: vscodeで`dotnet new console`したものとなっております。
|
127
|
+
現在試している環境: vscodeで`dotnet new console`したものとなっております。
|
128
|
+
|
129
|
+
### 得られた回答から少し修正を加えたもの
|
130
|
+
グループ化した結果を最終的な出力にもっていくために、`grouped`をforeachして
|
131
|
+
国名のキーチェック、名前のキーチェックをして詰め直す作業をしていますが、
|
132
|
+
この方法もやはり冗長な気がしていて、Selectで射影する際にToDictionaryできないものかと
|
133
|
+
試行錯誤をしています。
|
134
|
+
(.GroupByの後、.Selectして.ToDictinaryする or 単にToDictionaryで結果を得られないだろうか)
|
135
|
+
|
136
|
+
```C#
|
137
|
+
using System;
|
138
|
+
using System.Collections.Generic;
|
139
|
+
using System.Linq;
|
140
|
+
using System.Text.Json;
|
141
|
+
|
142
|
+
namespace test
|
143
|
+
{
|
144
|
+
public class Another
|
145
|
+
{
|
146
|
+
public class Human
|
147
|
+
{
|
148
|
+
public string Country { get; set; }
|
149
|
+
public string Name { get; set; }
|
150
|
+
public uint Login { get; set; }
|
151
|
+
}
|
152
|
+
|
153
|
+
public IEnumerable<Human> GetHumans()
|
154
|
+
{
|
155
|
+
yield return new Human
|
156
|
+
{
|
157
|
+
Country = "JP",
|
158
|
+
Name = "Taro",
|
159
|
+
Login = 10,
|
160
|
+
};
|
161
|
+
yield return new Human
|
162
|
+
{
|
163
|
+
Country = "JP",
|
164
|
+
Name = "Jiro",
|
165
|
+
Login = 5,
|
166
|
+
};
|
167
|
+
yield return new Human
|
168
|
+
{
|
169
|
+
Country = "US",
|
170
|
+
Name = "Mike",
|
171
|
+
Login = 20,
|
172
|
+
};
|
173
|
+
yield return new Human
|
174
|
+
{
|
175
|
+
Country = "JP",
|
176
|
+
Name = "Taro",
|
177
|
+
Login = 1,
|
178
|
+
};
|
179
|
+
}
|
180
|
+
}
|
181
|
+
|
182
|
+
class Program
|
183
|
+
{
|
184
|
+
static void Main(string[] args)
|
185
|
+
{
|
186
|
+
var another = new Another();
|
187
|
+
var dict = new Dictionary<string, Dictionary<string, uint>>();
|
188
|
+
foreach (var human in another.GetHumans())
|
189
|
+
{
|
190
|
+
if (dict.ContainsKey(human.Country))
|
191
|
+
if (dict[human.Country].ContainsKey(human.Name))
|
192
|
+
dict[human.Country][human.Name] += human.Login;
|
193
|
+
else
|
194
|
+
dict[human.Country].Add(human.Name, human.Login);
|
195
|
+
else
|
196
|
+
dict.Add(
|
197
|
+
human.Country, new Dictionary<string, uint>
|
198
|
+
{
|
199
|
+
[human.Name] = human.Login,
|
200
|
+
});
|
201
|
+
}
|
202
|
+
Console.WriteLine(dict);
|
203
|
+
|
204
|
+
var grouped = another.GetHumans()
|
205
|
+
.GroupBy(h => new { Country = h.Country, Name = h.Name })
|
206
|
+
.Select(g => new Another.Human
|
207
|
+
{
|
208
|
+
Country = g.Key.Country,
|
209
|
+
Name = g.Key.Name,
|
210
|
+
Login = (uint)g.Sum(x => x.Login)
|
211
|
+
});
|
212
|
+
var json = JsonSerializer.Serialize(grouped);
|
213
|
+
Console.WriteLine(json);
|
214
|
+
|
215
|
+
var dict2 = new Dictionary<string, Dictionary<string, uint>>();
|
216
|
+
foreach (var item in grouped)
|
217
|
+
if (dict2.ContainsKey(item.Country))
|
218
|
+
dict2[item.Country].Add(item.Name, item.Login);
|
219
|
+
else
|
220
|
+
dict2.Add(
|
221
|
+
item.Country, new Dictionary<string, uint>
|
222
|
+
{
|
223
|
+
[item.Name] = item.Login,
|
224
|
+
});
|
225
|
+
var json2 = JsonSerializer.Serialize(dict2);
|
226
|
+
Console.WriteLine(json2);
|
227
|
+
}
|
228
|
+
}
|
229
|
+
}
|
230
|
+
```
|
1
開発環境と得られる辞書情報を追記。
title
CHANGED
File without changes
|
body
CHANGED
@@ -17,6 +17,24 @@
|
|
17
17
|
その情報を辞書化するにあたっての処理が泥臭いと感じています。
|
18
18
|
(国名があれば名前をチェックし、存在すれば+=、そうでなければ辞書をAddする)
|
19
19
|
|
20
|
+
### 開発環境
|
21
|
+
OS: Windows10
|
22
|
+
.NET Core ランタイムのバージョン: .NET SDK (5.0.100) // 3.1でもよい。
|
23
|
+
最近新規作成したプロジェクトなので、バージョンの制約はないです。
|
24
|
+
|
25
|
+
### 得られる辞書情報
|
26
|
+
```Json
|
27
|
+
{
|
28
|
+
"JP": {
|
29
|
+
"Taro": 11,
|
30
|
+
"Jiro": 5
|
31
|
+
},
|
32
|
+
"US": {
|
33
|
+
"Mike": 20
|
34
|
+
}
|
35
|
+
}
|
36
|
+
```
|
37
|
+
|
20
38
|
### 該当のソースコード
|
21
39
|
|
22
40
|
```C#
|