回答編集履歴
1
言語指定のミスを修正
test
CHANGED
@@ -1,135 +1,68 @@
|
|
1
1
|
`DefaultIfEmpty`を使うと、外部結合に相当する挙動にできます。
|
2
2
|
|
3
|
+
```c#
|
4
|
+
class Program {
|
5
|
+
static void Main(string[] args) {
|
6
|
+
Trader[] traders = new Trader[] {
|
7
|
+
new Trader() { Id = 1, CompanyNm = "株式会社ABC", Address = "北海道" },
|
8
|
+
new Trader() { Id = 2, CompanyNm = "株式会社DEF", Address = "青森" },
|
9
|
+
new Trader() { Id = 3, CompanyNm = "株式会社GHI", Address = "秋田" },
|
10
|
+
new Trader() { Id = 4, CompanyNm = "株式会社HIJ", Address = "岩手" },
|
11
|
+
new Trader() { Id = 5, CompanyNm = "株式会社KLM", Address = "山形" },
|
12
|
+
new Trader() { Id = 6, CompanyNm = "株式会社OPQ", Address = "宮城" },
|
13
|
+
};
|
3
14
|
|
4
|
-
|
5
|
-
```lang-c#
|
6
|
-
|
7
|
-
class Program {
|
8
|
-
|
9
|
-
static void Main(string[] args) {
|
10
|
-
|
11
|
-
|
15
|
+
ItemList[] items = new ItemList[] {
|
12
|
-
|
13
|
-
new
|
16
|
+
new ItemList() { Id = 1, Price = 100, ExpiryDate = new DateTime(2000, 4, 1) },
|
14
|
-
|
15
|
-
new
|
17
|
+
new ItemList() { Id = 2, Price = 200, ExpiryDate = new DateTime(2005, 5, 1) },
|
16
|
-
|
17
|
-
new
|
18
|
+
new ItemList() { Id = 3, Price = 300, ExpiryDate = new DateTime(2010, 6, 1) },
|
18
|
-
|
19
|
-
new
|
19
|
+
new ItemList() { Id = 4, Price = 400, ExpiryDate = new DateTime(2015, 7, 1) },
|
20
|
-
|
21
|
-
new
|
20
|
+
new ItemList() { Id = 5, Price = 2020, ExpiryDate = new DateTime(2020, 8, 1) }
|
22
|
-
|
23
|
-
new Trader() { Id = 6, CompanyNm = "株式会社OPQ", Address = "宮城" },
|
24
|
-
|
25
21
|
};
|
26
22
|
|
27
23
|
|
28
|
-
|
29
|
-
ItemList[] items = new ItemList[] {
|
30
|
-
|
31
|
-
new ItemList() { Id = 1, Price = 100, ExpiryDate = new DateTime(2000, 4, 1) },
|
32
|
-
|
33
|
-
new ItemList() { Id = 2, Price = 200, ExpiryDate = new DateTime(2005, 5, 1) },
|
34
|
-
|
35
|
-
new ItemList() { Id = 3, Price = 300, ExpiryDate = new DateTime(2010, 6, 1) },
|
36
|
-
|
37
|
-
new ItemList() { Id = 4, Price = 400, ExpiryDate = new DateTime(2015, 7, 1) },
|
38
|
-
|
39
|
-
new ItemList() { Id = 5, Price = 2020, ExpiryDate = new DateTime(2020, 8, 1) }
|
40
|
-
|
41
|
-
};
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
24
|
var result = traders.GroupJoin(
|
48
|
-
|
49
25
|
items,
|
50
|
-
|
51
26
|
trader => trader.Id,
|
52
|
-
|
53
27
|
item => item.Id,
|
54
|
-
|
55
28
|
(trader, item) => new { trader, item })
|
56
|
-
|
57
29
|
.SelectMany(
|
58
|
-
|
59
30
|
o => o.item.DefaultIfEmpty(), // traderに結合されるitemがない場合は、itemをnullにする
|
60
|
-
|
61
31
|
(j, k) => new {
|
62
|
-
|
63
32
|
Id = j.trader.Id,
|
64
|
-
|
65
33
|
CompanyNm = j.trader.CompanyNm,
|
66
|
-
|
67
34
|
Address = j.trader.Address,
|
68
|
-
|
69
35
|
Price = k?.Price, // itemがnullの場合はPrice/ExpiryDateは参照しない
|
70
|
-
|
71
36
|
ExpireDate = k?.ExpiryDate
|
72
|
-
|
73
37
|
}
|
74
|
-
|
75
38
|
);
|
76
39
|
|
77
|
-
|
78
|
-
|
79
40
|
foreach (var o in result) {
|
80
|
-
|
81
41
|
Console.WriteLine($"Id={o.Id}, CompanyNm={o.CompanyNm}, Address={o.Address}, Price={o.Price}, ExpireDate={o.ExpireDate}");
|
82
|
-
|
83
42
|
}
|
84
|
-
|
85
43
|
}
|
86
44
|
|
87
|
-
|
88
|
-
|
89
45
|
class Trader {
|
90
|
-
|
91
46
|
public int Id { get; set; }
|
92
|
-
|
93
47
|
public string CompanyNm { get; set; }
|
94
|
-
|
95
48
|
public string Address { get; set; }
|
96
|
-
|
97
49
|
}
|
98
50
|
|
99
|
-
|
100
|
-
|
101
51
|
class ItemList {
|
102
|
-
|
103
52
|
public int Id { get; set; }
|
104
|
-
|
105
53
|
public int Price { get; set; }
|
106
|
-
|
107
54
|
public DateTime ExpiryDate { get; set; }
|
108
|
-
|
109
55
|
}
|
110
|
-
|
111
56
|
}
|
112
|
-
|
113
57
|
```
|
114
|
-
|
115
|
-
|
116
58
|
|
117
59
|
出力。
|
118
60
|
|
119
|
-
|
120
|
-
|
121
61
|
```
|
122
|
-
|
123
62
|
Id=1, CompanyNm=株式会社ABC, Address=北海道, Price=100, ExpireDate=00/04/01 0:00:00
|
124
|
-
|
125
63
|
Id=2, CompanyNm=株式会社DEF, Address=青森, Price=200, ExpireDate=05/05/01 0:00:00
|
126
|
-
|
127
64
|
Id=3, CompanyNm=株式会社GHI, Address=秋田, Price=300, ExpireDate=10/06/01 0:00:00
|
128
|
-
|
129
65
|
Id=4, CompanyNm=株式会社HIJ, Address=岩手, Price=400, ExpireDate=15/07/01 0:00:00
|
130
|
-
|
131
66
|
Id=5, CompanyNm=株式会社KLM, Address=山形, Price=2020, ExpireDate=20/08/01 0:00:00
|
132
|
-
|
133
67
|
Id=6, CompanyNm=株式会社OPQ, Address=宮城, Price=, ExpireDate=
|
134
|
-
|
135
68
|
```
|