回答編集履歴
3
言語指定の誤りを修正
test
CHANGED
@@ -1,141 +1,71 @@
|
|
1
1
|
こんな感じでどうでしょうか。
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
```
|
3
|
+
```c#
|
6
|
-
|
7
4
|
using System;
|
8
|
-
|
9
5
|
using System.Linq;
|
10
6
|
|
11
|
-
|
12
|
-
|
13
7
|
namespace LinqSample {
|
14
|
-
|
15
8
|
class Program {
|
16
|
-
|
17
9
|
static void Main(string[] args) {
|
18
|
-
|
19
10
|
var traders = new Trader[] {
|
20
|
-
|
21
11
|
new Trader() { Id = 1, CompanyNm = "株式会社ABC", Address = "北海道" },
|
22
|
-
|
23
12
|
new Trader() { Id = 2, CompanyNm = "株式会社DEF", Address = "青森" },
|
24
|
-
|
25
13
|
new Trader() { Id = 3, CompanyNm = "株式会社GHI", Address = "秋田" },
|
26
|
-
|
27
14
|
new Trader() { Id = 4, CompanyNm = "株式会社HIJ", Address = "岩手" },
|
28
|
-
|
29
15
|
new Trader() { Id = 5, CompanyNm = "株式会社KLM", Address = "山形" },
|
30
|
-
|
31
16
|
new Trader() { Id = 6, CompanyNm = "株式会社OPQ", Address = "宮城" },
|
32
|
-
|
33
17
|
};
|
34
18
|
|
35
|
-
|
36
|
-
|
37
19
|
var items = new ItemList[] {
|
38
|
-
|
39
20
|
new ItemList() { Id = 1, Price = 100, ExpiryDate = new DateTime(2000, 4, 1) },
|
40
|
-
|
41
21
|
new ItemList() { Id = 2, Price = 200, ExpiryDate = new DateTime(2005, 5, 1) },
|
42
|
-
|
43
22
|
new ItemList() { Id = 3, Price = 300, ExpiryDate = new DateTime(2010, 6, 1) },
|
44
|
-
|
45
23
|
new ItemList() { Id = 4, Price = 400, ExpiryDate = new DateTime(2015, 7, 1) },
|
46
|
-
|
47
24
|
new ItemList() { Id = 5, Price = 2020, ExpiryDate = new DateTime(2020, 8, 1) }
|
48
|
-
|
49
25
|
};
|
50
26
|
|
51
|
-
|
52
|
-
|
53
27
|
var result = traders.GroupJoin(
|
54
|
-
|
55
28
|
items,
|
56
|
-
|
57
29
|
trader => trader.Id,
|
58
|
-
|
59
30
|
item => item.Id,
|
60
|
-
|
61
31
|
(trader, item) => new { trader, item })
|
62
|
-
|
63
32
|
// シーケンスの展開
|
64
|
-
|
65
33
|
.SelectMany(traderAndItem => traderAndItem.item, (trader, item) => new {
|
66
|
-
|
67
34
|
Id = trader.trader.Id,
|
68
|
-
|
69
35
|
CompanyNm = trader.trader.CompanyNm,
|
70
|
-
|
71
36
|
Address = trader.trader.Address,
|
72
|
-
|
73
37
|
Price = item.Price,
|
74
|
-
|
75
38
|
ExpireDate = item.ExpiryDate
|
76
|
-
|
77
39
|
})
|
78
|
-
|
79
40
|
.Where(o => o.ExpireDate > new DateTime(2010, 1, 1))
|
80
|
-
|
81
41
|
.ToList();
|
82
42
|
|
83
|
-
|
84
|
-
|
85
43
|
foreach (var o in result) {
|
86
|
-
|
87
44
|
Console.WriteLine($"Id={o.Id}, CompanyNm={o.CompanyNm}, Address={o.Address}, Price={o.Price}, ExpireDate={o.ExpireDate}");
|
88
|
-
|
89
45
|
}
|
90
|
-
|
91
46
|
}
|
92
|
-
|
93
47
|
}
|
94
48
|
|
95
|
-
|
96
|
-
|
97
49
|
class Trader {
|
98
|
-
|
99
50
|
public int Id { get; set; }
|
100
|
-
|
101
51
|
public string CompanyNm { get; set; }
|
102
|
-
|
103
52
|
public string Address { get; set; }
|
104
|
-
|
105
53
|
}
|
106
54
|
|
107
|
-
|
108
|
-
|
109
55
|
class ItemList {
|
110
|
-
|
111
56
|
public int Id { get; set; }
|
112
|
-
|
113
57
|
public int Price { get; set; }
|
114
|
-
|
115
58
|
public DateTime ExpiryDate { get; set; }
|
116
|
-
|
117
59
|
}
|
118
|
-
|
119
60
|
}
|
120
|
-
|
121
|
-
|
122
|
-
|
123
61
|
|
124
62
|
|
125
63
|
```
|
126
64
|
|
127
|
-
|
128
|
-
|
129
65
|
出力。
|
130
66
|
|
131
|
-
|
132
|
-
|
133
67
|
```
|
134
|
-
|
135
68
|
Id=3, CompanyNm=株式会社GHI, Address=秋田, Price=300, ExpireDate=10/06/01 0:00:00
|
136
|
-
|
137
69
|
Id=4, CompanyNm=株式会社HIJ, Address=岩手, Price=400, ExpireDate=15/07/01 0:00:00
|
138
|
-
|
139
70
|
Id=5, CompanyNm=株式会社KLM, Address=山形, Price=2020, ExpireDate=20/08/01 0:00:00
|
140
|
-
|
141
71
|
```
|
2
refactor
test
CHANGED
@@ -16,7 +16,7 @@
|
|
16
16
|
|
17
17
|
static void Main(string[] args) {
|
18
18
|
|
19
|
-
|
19
|
+
var traders = new Trader[] {
|
20
20
|
|
21
21
|
new Trader() { Id = 1, CompanyNm = "株式会社ABC", Address = "北海道" },
|
22
22
|
|
@@ -34,7 +34,7 @@
|
|
34
34
|
|
35
35
|
|
36
36
|
|
37
|
-
|
37
|
+
var items = new ItemList[] {
|
38
38
|
|
39
39
|
new ItemList() { Id = 1, Price = 100, ExpiryDate = new DateTime(2000, 4, 1) },
|
40
40
|
|
@@ -62,17 +62,17 @@
|
|
62
62
|
|
63
63
|
// シーケンスの展開
|
64
64
|
|
65
|
-
.SelectMany(
|
65
|
+
.SelectMany(traderAndItem => traderAndItem.item, (trader, item) => new {
|
66
66
|
|
67
|
-
Id =
|
67
|
+
Id = trader.trader.Id,
|
68
68
|
|
69
|
-
CompanyNm =
|
69
|
+
CompanyNm = trader.trader.CompanyNm,
|
70
70
|
|
71
|
-
Address =
|
71
|
+
Address = trader.trader.Address,
|
72
72
|
|
73
|
-
Price =
|
73
|
+
Price = item.Price,
|
74
74
|
|
75
|
-
ExpireDate =
|
75
|
+
ExpireDate = item.ExpiryDate
|
76
76
|
|
77
77
|
})
|
78
78
|
|
@@ -118,6 +118,10 @@
|
|
118
118
|
|
119
119
|
}
|
120
120
|
|
121
|
+
|
122
|
+
|
123
|
+
|
124
|
+
|
121
125
|
```
|
122
126
|
|
123
127
|
|
1
ToListを追加
test
CHANGED
@@ -76,7 +76,9 @@
|
|
76
76
|
|
77
77
|
})
|
78
78
|
|
79
|
-
.Where(o => o.ExpireDate > new DateTime(2010, 1, 1))
|
79
|
+
.Where(o => o.ExpireDate > new DateTime(2010, 1, 1))
|
80
|
+
|
81
|
+
.ToList();
|
80
82
|
|
81
83
|
|
82
84
|
|
@@ -116,8 +118,6 @@
|
|
116
118
|
|
117
119
|
}
|
118
120
|
|
119
|
-
|
120
|
-
|
121
121
|
```
|
122
122
|
|
123
123
|
|