質問編集履歴

4

得たい辞書情報を追加(外からデータ構造にアクセスできるように、フィールド名を持たせた。)入出力条件を追加。

2021/03/21 07:58

投稿

rtazaki
rtazaki

スコア69

test CHANGED
File without changes
test CHANGED
@@ -72,7 +72,63 @@
72
72
 
73
73
 
74
74
 
75
+ ### 得たい辞書情報
76
+
77
+ ```Json
78
+
79
+ [
80
+
81
+ {
82
+
83
+ "Country": "JP",
84
+
85
+ "PersonalData": [
86
+
87
+ {
88
+
89
+ "Name": "Taro",
90
+
91
+ "Login": 11
92
+
93
+ },
94
+
95
+ {
96
+
97
+ "Name": "Jiro",
98
+
99
+ "Login": 5
100
+
101
+ }
102
+
103
+ ]
104
+
105
+ },
106
+
107
+ {
108
+
109
+ "Country": "US",
110
+
111
+ "PersonalData": [
112
+
113
+ {
114
+
115
+ "Name": "Mike",
116
+
117
+ "Login": 20
118
+
119
+ }
120
+
121
+ ]
122
+
123
+ }
124
+
125
+ ]
126
+
127
+ ```
128
+
129
+
130
+
75
- ### 該当のソースコード
131
+ ### 該当のソースコード(元コード)
76
132
 
77
133
 
78
134
 
@@ -457,3 +513,11 @@
457
513
  }
458
514
 
459
515
  ```
516
+
517
+
518
+
519
+ ### さらに追記
520
+
521
+ * 辞書は外部にてJsonデータとして使えるようにフィールド名を持つこと。
522
+
523
+ * AnotherからIEnumerableで1件ずつ来る仕様は変えてはならない。

3

GetHumansの中でという表現だと、情報を持つ側で重複削除&加算したものを返すという誤解がありそうだったため、修正。

2021/03/21 07:58

投稿

rtazaki
rtazaki

スコア69

test CHANGED
File without changes
test CHANGED
@@ -20,7 +20,7 @@
20
20
 
21
21
  * `Programクラス`
22
22
 
23
- * GetHumansの中で、国名、名前、ログイン回数の多段辞書を作る。(Jsonとして外に出す情報)
23
+ * GetHumans~~の中で、~~から国名、名前、ログイン回数の多段辞書を作る。(Jsonとして外に出す情報)
24
24
 
25
25
  ** 国名、名前が等しい場合、ログイン回数は加算する。**
26
26
 

2

得られた回答から少し修正を加えたものを追記

2021/03/21 04:26

投稿

rtazaki
rtazaki

スコア69

test CHANGED
File without changes
test CHANGED
@@ -251,3 +251,209 @@
251
251
  ### 補足情報
252
252
 
253
253
  現在試している環境: vscodeで`dotnet new console`したものとなっております。
254
+
255
+
256
+
257
+ ### 得られた回答から少し修正を加えたもの
258
+
259
+ グループ化した結果を最終的な出力にもっていくために、`grouped`をforeachして
260
+
261
+ 国名のキーチェック、名前のキーチェックをして詰め直す作業をしていますが、
262
+
263
+ この方法もやはり冗長な気がしていて、Selectで射影する際にToDictionaryできないものかと
264
+
265
+ 試行錯誤をしています。
266
+
267
+ (.GroupByの後、.Selectして.ToDictinaryする or 単にToDictionaryで結果を得られないだろうか)
268
+
269
+
270
+
271
+ ```C#
272
+
273
+ using System;
274
+
275
+ using System.Collections.Generic;
276
+
277
+ using System.Linq;
278
+
279
+ using System.Text.Json;
280
+
281
+
282
+
283
+ namespace test
284
+
285
+ {
286
+
287
+ public class Another
288
+
289
+ {
290
+
291
+ public class Human
292
+
293
+ {
294
+
295
+ public string Country { get; set; }
296
+
297
+ public string Name { get; set; }
298
+
299
+ public uint Login { get; set; }
300
+
301
+ }
302
+
303
+
304
+
305
+ public IEnumerable<Human> GetHumans()
306
+
307
+ {
308
+
309
+ yield return new Human
310
+
311
+ {
312
+
313
+ Country = "JP",
314
+
315
+ Name = "Taro",
316
+
317
+ Login = 10,
318
+
319
+ };
320
+
321
+ yield return new Human
322
+
323
+ {
324
+
325
+ Country = "JP",
326
+
327
+ Name = "Jiro",
328
+
329
+ Login = 5,
330
+
331
+ };
332
+
333
+ yield return new Human
334
+
335
+ {
336
+
337
+ Country = "US",
338
+
339
+ Name = "Mike",
340
+
341
+ Login = 20,
342
+
343
+ };
344
+
345
+ yield return new Human
346
+
347
+ {
348
+
349
+ Country = "JP",
350
+
351
+ Name = "Taro",
352
+
353
+ Login = 1,
354
+
355
+ };
356
+
357
+ }
358
+
359
+ }
360
+
361
+
362
+
363
+ class Program
364
+
365
+ {
366
+
367
+ static void Main(string[] args)
368
+
369
+ {
370
+
371
+ var another = new Another();
372
+
373
+ var dict = new Dictionary<string, Dictionary<string, uint>>();
374
+
375
+ foreach (var human in another.GetHumans())
376
+
377
+ {
378
+
379
+ if (dict.ContainsKey(human.Country))
380
+
381
+ if (dict[human.Country].ContainsKey(human.Name))
382
+
383
+ dict[human.Country][human.Name] += human.Login;
384
+
385
+ else
386
+
387
+ dict[human.Country].Add(human.Name, human.Login);
388
+
389
+ else
390
+
391
+ dict.Add(
392
+
393
+ human.Country, new Dictionary<string, uint>
394
+
395
+ {
396
+
397
+ [human.Name] = human.Login,
398
+
399
+ });
400
+
401
+ }
402
+
403
+ Console.WriteLine(dict);
404
+
405
+
406
+
407
+ var grouped = another.GetHumans()
408
+
409
+ .GroupBy(h => new { Country = h.Country, Name = h.Name })
410
+
411
+ .Select(g => new Another.Human
412
+
413
+ {
414
+
415
+ Country = g.Key.Country,
416
+
417
+ Name = g.Key.Name,
418
+
419
+ Login = (uint)g.Sum(x => x.Login)
420
+
421
+ });
422
+
423
+ var json = JsonSerializer.Serialize(grouped);
424
+
425
+ Console.WriteLine(json);
426
+
427
+
428
+
429
+ var dict2 = new Dictionary<string, Dictionary<string, uint>>();
430
+
431
+ foreach (var item in grouped)
432
+
433
+ if (dict2.ContainsKey(item.Country))
434
+
435
+ dict2[item.Country].Add(item.Name, item.Login);
436
+
437
+ else
438
+
439
+ dict2.Add(
440
+
441
+ item.Country, new Dictionary<string, uint>
442
+
443
+ {
444
+
445
+ [item.Name] = item.Login,
446
+
447
+ });
448
+
449
+ var json2 = JsonSerializer.Serialize(dict2);
450
+
451
+ Console.WriteLine(json2);
452
+
453
+ }
454
+
455
+ }
456
+
457
+ }
458
+
459
+ ```

1

開発環境と得られる辞書情報を追記。

2021/03/21 04:15

投稿

rtazaki
rtazaki

スコア69

test CHANGED
File without changes
test CHANGED
@@ -36,6 +36,42 @@
36
36
 
37
37
 
38
38
 
39
+ ### 開発環境
40
+
41
+ OS: Windows10
42
+
43
+ .NET Core ランタイムのバージョン: .NET SDK (5.0.100) // 3.1でもよい。
44
+
45
+ 最近新規作成したプロジェクトなので、バージョンの制約はないです。
46
+
47
+
48
+
49
+ ### 得られる辞書情報
50
+
51
+ ```Json
52
+
53
+ {
54
+
55
+ "JP": {
56
+
57
+ "Taro": 11,
58
+
59
+ "Jiro": 5
60
+
61
+ },
62
+
63
+ "US": {
64
+
65
+ "Mike": 20
66
+
67
+ }
68
+
69
+ }
70
+
71
+ ```
72
+
73
+
74
+
39
75
  ### 該当のソースコード
40
76
 
41
77