回答編集履歴

5

追記

2020/12/03 06:49

投稿

BluOxy
BluOxy

スコア2663

test CHANGED
@@ -12,7 +12,7 @@
12
12
 
13
13
 
14
14
 
15
- ※接続するプロバイダーによってはGroupByがSQL等の命令に変換できず、[Client side GroupBy is not supported](https://stackoverflow.com/questions/58138556/client-side-groupby-is-not-supported) 等のエラーが出るため、その場合はEntityのコレクションを取得し、ToListメソッド等でIEnumerableに変換した上でLINQ to ObjectsのGroupByを使ってください。
15
+ ※接続するプロバイダーによってはGroupByがSQL等の命令に変換できず、[Client side GroupBy is not supported](https://stackoverflow.com/questions/58138556/client-side-groupby-is-not-supported) 等のエラーが出るため、その場合はEntityのコレクションを取得し、ToListメソッド等でIEnumerableに変換した上でLINQ to EntitiesではなくLINQ to ObjectsのGroupByを使ってください。
16
16
 
17
17
 
18
18
 

4

なぜか修正が反映されていない

2020/12/03 06:49

投稿

BluOxy
BluOxy

スコア2663

test CHANGED
File without changes

3

なぜか修正が反映されていない

2020/12/03 06:39

投稿

BluOxy
BluOxy

スコア2663

test CHANGED
@@ -12,7 +12,7 @@
12
12
 
13
13
 
14
14
 
15
- ※接続するプロバイダーによってはGroupByがSQL等の命令に変換できず、[Client side GroupBy is not supported](https://stackoverflow.com/questions/58138556/client-side-groupby-is-not-supported) 等のエラーが出るため、その場合はEntityのコレクションを取得し、IEnumerableに変換した上でLINQ to ObjectsのGroupByを使ってください。
15
+ ※接続するプロバイダーによってはGroupByがSQL等の命令に変換できず、[Client side GroupBy is not supported](https://stackoverflow.com/questions/58138556/client-side-groupby-is-not-supported) 等のエラーが出るため、その場合はEntityのコレクションを取得し、ToListメソッド等でIEnumerableに変換した上でLINQ to ObjectsのGroupByを使ってください。
16
16
 
17
17
 
18
18
 

2

エラーが出る件を追記(要コメント欄確認)

2020/12/03 06:39

投稿

BluOxy
BluOxy

スコア2663

test CHANGED
@@ -6,10 +6,16 @@
6
6
 
7
7
  LINQ to Entitiesの [Queryable.GroupBy メソッド](https://docs.microsoft.com/ja-jp/dotnet/api/system.linq.queryable.groupby?view=net-5.0)を使ってください。
8
8
 
9
+
10
+
9
11
  Entity Frameworkを使用してデータベースからEntityのコレクションを取得できるように環境を整える必要があります。
10
12
 
11
13
 
12
14
 
15
+ ※接続するプロバイダーによってはGroupByがSQL等の命令に変換できず、[Client side GroupBy is not supported](https://stackoverflow.com/questions/58138556/client-side-groupby-is-not-supported) 等のエラーが出るため、その場合はEntityのコレクションを取得し、IEnumerableに変換した上でLINQ to ObjectsのGroupByを使ってください。
16
+
17
+
18
+
13
19
  > しかしそれを実装するにしてもその処理をどこに記述するべきなのか(Controller? View?)わかっていません。
14
20
 
15
21
  > View側(cshtmlかjavascript)でデータの編集をしても良いのでしょうか?

1

ソースコード追加

2020/12/03 06:37

投稿

BluOxy
BluOxy

スコア2663

test CHANGED
@@ -45,3 +45,421 @@
45
45
  - Razor構文を使用してforeachで各グループ毎にtableタグを使い表を作る
46
46
 
47
47
  - それぞれのtableタグでグループが持つ各要素毎にtr,thタグなどで行を追加&行にデータを書き込む
48
+
49
+
50
+
51
+ # Sample
52
+
53
+
54
+
55
+ EFからスキャフォールディングで生成したControllerをベースに、Indexメソッドだけ弄ってます。
56
+
57
+
58
+
59
+ ```C#
60
+
61
+ using System;
62
+
63
+ using System.Collections.Generic;
64
+
65
+ using System.Linq;
66
+
67
+ using System.Threading.Tasks;
68
+
69
+ using Microsoft.AspNetCore.Mvc;
70
+
71
+ using Microsoft.AspNetCore.Mvc.Rendering;
72
+
73
+ using Microsoft.EntityFrameworkCore;
74
+
75
+ using WebApplication3.Models;
76
+
77
+
78
+
79
+ namespace WebApplication3.Controllers
80
+
81
+ {
82
+
83
+ public class SampleController : Controller
84
+
85
+ {
86
+
87
+ private readonly SampleContext _context;
88
+
89
+
90
+
91
+ public SampleController(SampleContext context)
92
+
93
+ {
94
+
95
+ _context = context;
96
+
97
+ }
98
+
99
+
100
+
101
+ // GET: Sample
102
+
103
+ public async Task<IActionResult> Index()
104
+
105
+ {
106
+
107
+ return View(await _context.SampleDataModels.GroupBy(x => x.Identifier).ToListAsync());
108
+
109
+ }
110
+
111
+
112
+
113
+ // GET: Sample/Details/5
114
+
115
+ public async Task<IActionResult> Details(int? id)
116
+
117
+ {
118
+
119
+ if (id == null)
120
+
121
+ {
122
+
123
+ return NotFound();
124
+
125
+ }
126
+
127
+
128
+
129
+ var sampleDataModel = await _context.SampleDataModels
130
+
131
+ .FirstOrDefaultAsync(m => m.Id == id);
132
+
133
+ if (sampleDataModel == null)
134
+
135
+ {
136
+
137
+ return NotFound();
138
+
139
+ }
140
+
141
+
142
+
143
+ return View(sampleDataModel);
144
+
145
+ }
146
+
147
+
148
+
149
+ // GET: Sample/Create
150
+
151
+ public IActionResult Create()
152
+
153
+ {
154
+
155
+ return View();
156
+
157
+ }
158
+
159
+
160
+
161
+ // POST: Sample/Create
162
+
163
+ // To protect from overposting attacks, please enable the specific properties you want to bind to, for
164
+
165
+ // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
166
+
167
+ [HttpPost]
168
+
169
+ [ValidateAntiForgeryToken]
170
+
171
+ public async Task<IActionResult> Create([Bind("Id,Identifier,Name")] SampleDataModel sampleDataModel)
172
+
173
+ {
174
+
175
+ if (ModelState.IsValid)
176
+
177
+ {
178
+
179
+ _context.Add(sampleDataModel);
180
+
181
+ await _context.SaveChangesAsync();
182
+
183
+ return RedirectToAction(nameof(Index));
184
+
185
+ }
186
+
187
+ return View(sampleDataModel);
188
+
189
+ }
190
+
191
+
192
+
193
+ // GET: Sample/Edit/5
194
+
195
+ public async Task<IActionResult> Edit(int? id)
196
+
197
+ {
198
+
199
+ if (id == null)
200
+
201
+ {
202
+
203
+ return NotFound();
204
+
205
+ }
206
+
207
+
208
+
209
+ var sampleDataModel = await _context.SampleDataModels.FindAsync(id);
210
+
211
+ if (sampleDataModel == null)
212
+
213
+ {
214
+
215
+ return NotFound();
216
+
217
+ }
218
+
219
+ return View(sampleDataModel);
220
+
221
+ }
222
+
223
+
224
+
225
+ // POST: Sample/Edit/5
226
+
227
+ // To protect from overposting attacks, please enable the specific properties you want to bind to, for
228
+
229
+ // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
230
+
231
+ [HttpPost]
232
+
233
+ [ValidateAntiForgeryToken]
234
+
235
+ public async Task<IActionResult> Edit(int id, [Bind("Id,Identifier,Name")] SampleDataModel sampleDataModel)
236
+
237
+ {
238
+
239
+ if (id != sampleDataModel.Id)
240
+
241
+ {
242
+
243
+ return NotFound();
244
+
245
+ }
246
+
247
+
248
+
249
+ if (ModelState.IsValid)
250
+
251
+ {
252
+
253
+ try
254
+
255
+ {
256
+
257
+ _context.Update(sampleDataModel);
258
+
259
+ await _context.SaveChangesAsync();
260
+
261
+ }
262
+
263
+ catch (DbUpdateConcurrencyException)
264
+
265
+ {
266
+
267
+ if (!SampleDataModelExists(sampleDataModel.Id))
268
+
269
+ {
270
+
271
+ return NotFound();
272
+
273
+ }
274
+
275
+ else
276
+
277
+ {
278
+
279
+ throw;
280
+
281
+ }
282
+
283
+ }
284
+
285
+ return RedirectToAction(nameof(Index));
286
+
287
+ }
288
+
289
+ return View(sampleDataModel);
290
+
291
+ }
292
+
293
+
294
+
295
+ // GET: Sample/Delete/5
296
+
297
+ public async Task<IActionResult> Delete(int? id)
298
+
299
+ {
300
+
301
+ if (id == null)
302
+
303
+ {
304
+
305
+ return NotFound();
306
+
307
+ }
308
+
309
+
310
+
311
+ var sampleDataModel = await _context.SampleDataModels
312
+
313
+ .FirstOrDefaultAsync(m => m.Id == id);
314
+
315
+ if (sampleDataModel == null)
316
+
317
+ {
318
+
319
+ return NotFound();
320
+
321
+ }
322
+
323
+
324
+
325
+ return View(sampleDataModel);
326
+
327
+ }
328
+
329
+
330
+
331
+ // POST: Sample/Delete/5
332
+
333
+ [HttpPost, ActionName("Delete")]
334
+
335
+ [ValidateAntiForgeryToken]
336
+
337
+ public async Task<IActionResult> DeleteConfirmed(int id)
338
+
339
+ {
340
+
341
+ var sampleDataModel = await _context.SampleDataModels.FindAsync(id);
342
+
343
+ _context.SampleDataModels.Remove(sampleDataModel);
344
+
345
+ await _context.SaveChangesAsync();
346
+
347
+ return RedirectToAction(nameof(Index));
348
+
349
+ }
350
+
351
+
352
+
353
+ private bool SampleDataModelExists(int id)
354
+
355
+ {
356
+
357
+ return _context.SampleDataModels.Any(e => e.Id == id);
358
+
359
+ }
360
+
361
+ }
362
+
363
+ }
364
+
365
+ ```
366
+
367
+
368
+
369
+ ```cshtml
370
+
371
+ @model IEnumerable<IGrouping<string,WebApplication3.Models.SampleDataModel>>
372
+
373
+
374
+
375
+ @{
376
+
377
+ ViewData["Title"] = "Index";
378
+
379
+ }
380
+
381
+
382
+
383
+ <h2>Index</h2>
384
+
385
+
386
+
387
+ <p>
388
+
389
+ <a asp-action="Create">Create New</a>
390
+
391
+ </p>
392
+
393
+
394
+
395
+ @foreach (var group in Model)
396
+
397
+ {
398
+
399
+ <table class="table">
400
+
401
+ <thead>
402
+
403
+ <tr>
404
+
405
+ <th>
406
+
407
+ @Html.DisplayNameFor(model => model.First().Identifier)
408
+
409
+ </th>
410
+
411
+ <th>
412
+
413
+ @Html.DisplayNameFor(model => model.First().Name)
414
+
415
+ </th>
416
+
417
+ <th></th>
418
+
419
+ </tr>
420
+
421
+ </thead>
422
+
423
+ <tbody>
424
+
425
+ @foreach (var item in group)
426
+
427
+ {
428
+
429
+ <tr>
430
+
431
+ <td>
432
+
433
+ @Html.DisplayFor(modelItem => item.Identifier)
434
+
435
+ </td>
436
+
437
+ <td>
438
+
439
+ @Html.DisplayFor(modelItem => item.Name)
440
+
441
+ </td>
442
+
443
+ <td>
444
+
445
+ <a asp-action="Edit" asp-route-id="@item.Id">Edit</a> |
446
+
447
+ <a asp-action="Details" asp-route-id="@item.Id">Details</a> |
448
+
449
+ <a asp-action="Delete" asp-route-id="@item.Id">Delete</a>
450
+
451
+ </td>
452
+
453
+ </tr>
454
+
455
+ }
456
+
457
+ </tbody>
458
+
459
+ </table>
460
+
461
+ }
462
+
463
+ ```
464
+
465
+ ![イメージ説明](af2f32603d300c0b19978cff0d3728da.png)