質問するログイン新規登録

回答編集履歴

2

【追記2】追加

2017/11/24 08:58

投稿

退会済みユーザー
answer CHANGED
@@ -7,7 +7,7 @@
7
7
  左外部結合の実行
8
8
  [https://docs.microsoft.com/ja-jp/dotnet/csharp/linq/perform-left-outer-joins](https://docs.microsoft.com/ja-jp/dotnet/csharp/linq/perform-left-outer-joins)
9
9
 
10
- 【追記】
10
+ **【追記】**
11
11
 
12
12
  上記案でのサンプルを書いておきます。Oracle の方は Entity Framework を使えれば簡単に、CSV の方も DataSet / DataTable を作るのよりは多分少ない労力で List<T> 型のオブジェクトを作れると思います。
13
13
 
@@ -87,4 +87,174 @@
87
87
  </form>
88
88
  </body>
89
89
  </html>
90
+ ```
91
+
92
+ **【追記2】**
93
+
94
+ VB.NET に直したサンプルを下にアップしておきます。変換サービスで変換して動くように手直ししただけです。なので、VB.NET の書き方としてはアレかもしれませんが、期待通りの結果になることは確認済みです。
95
+
96
+
97
+ ```
98
+ <%@ Page Language="VB" %>
99
+
100
+ <!DOCTYPE html>
101
+
102
+ <script runat="server">
103
+
104
+ Public Class Employee
105
+ Public Property Id() As String
106
+ Get
107
+ Return m_Id
108
+ End Get
109
+ Set
110
+ m_Id = Value
111
+ End Set
112
+ End Property
113
+ Private m_Id As String
114
+ Public Property Dept() As String
115
+ Get
116
+ Return m_Dept
117
+ End Get
118
+ Set
119
+ m_Dept = Value
120
+ End Set
121
+ End Property
122
+ Private m_Dept As String
123
+ Public Property Name() As String
124
+ Get
125
+ Return m_Name
126
+ End Get
127
+ Set
128
+ m_Name = Value
129
+ End Set
130
+ End Property
131
+ Private m_Name As String
132
+ End Class
133
+
134
+ Public Class Role
135
+ Public Property Id() As String
136
+ Get
137
+ Return m_Id
138
+ End Get
139
+ Set
140
+ m_Id = Value
141
+ End Set
142
+ End Property
143
+ Private m_Id As String
144
+ Public Property Right() As String
145
+ Get
146
+ Return m_Right
147
+ End Get
148
+ Set
149
+ m_Right = Value
150
+ End Set
151
+ End Property
152
+ Private m_Right As String
153
+ End Class
154
+
155
+ Public Class JoinedTable
156
+ Public Property Id() As String
157
+ Get
158
+ Return m_Id
159
+ End Get
160
+ Set
161
+ m_Id = Value
162
+ End Set
163
+ End Property
164
+ Private m_Id As String
165
+ Public Property Dept() As String
166
+ Get
167
+ Return m_Dept
168
+ End Get
169
+ Set
170
+ m_Dept = Value
171
+ End Set
172
+ End Property
173
+ Private m_Dept As String
174
+ Public Property Name() As String
175
+ Get
176
+ Return m_Name
177
+ End Get
178
+ Set
179
+ m_Name = Value
180
+ End Set
181
+ End Property
182
+ Private m_Name As String
183
+ Public Property Right() As String
184
+ Get
185
+ Return m_Right
186
+ End Get
187
+ Set
188
+ m_Right = Value
189
+ End Set
190
+ End Property
191
+ Private m_Right As String
192
+ End Class
193
+
194
+
195
+ Protected Sub Page_Load(sender As Object, erg As EventArgs)
196
+ If Not IsPostBack Then
197
+ Dim employees As New List(Of Employee)() From {
198
+ New Employee() With {
199
+ .Id = "ID0001",
200
+ .Dept = "部署A",
201
+ .Name = "鈴木太郎"
202
+ },
203
+ New Employee() With {
204
+ .Id = "ID0002",
205
+ .Dept = "部署A",
206
+ .Name = "山田一郎"
207
+ },
208
+ New Employee() With {
209
+ .Id = "ID0003",
210
+ .Dept = "部署B",
211
+ .Name = "佐藤花子"
212
+ }
213
+ }
214
+
215
+ Dim roles As New List(Of Role)() From {
216
+ New Role() With {
217
+ .Id = "ID0001",
218
+ .Right = "〇"
219
+ },
220
+ New Role() With {
221
+ .Id = "ID0003",
222
+ .Right = "×"
223
+ },
224
+ New Role() With {
225
+ .Id = "ID0004",
226
+ .Right = "〇"
227
+ }
228
+ }
229
+
230
+ Dim query = From e In employees
231
+ Group Join r In roles
232
+ On e.Id Equals r.Id Into Group
233
+ From subrole In Group.DefaultIfEmpty()
234
+ Select New JoinedTable() With {
235
+ .Id = e.Id,
236
+ .Dept = e.Dept,
237
+ .Name = e.Name,
238
+ .Right = If(subrole Is Nothing, String.Empty, subrole.Right)
239
+ }
240
+
241
+ GridView1.DataSource = query
242
+ GridView1.DataBind()
243
+ End If
244
+ End Sub
245
+ </script>
246
+
247
+ <html xmlns="http://www.w3.org/1999/xhtml">
248
+ <head runat="server">
249
+ <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
250
+ <title></title>
251
+ </head>
252
+ <body>
253
+ <form id="form1" runat="server">
254
+ <div>
255
+ <asp:GridView ID="GridView1" runat="server"></asp:GridView>
256
+ </div>
257
+ </form>
258
+ </body>
259
+ </html>
90
260
  ```

1

サンプル追記

2017/11/24 08:58

投稿

退会済みユーザー
answer CHANGED
@@ -5,4 +5,86 @@
5
5
  DataSet / DataTable を作ってそれを操作しようとするより、以下のように List<T> 型として「テーブル1」と「テーブル2」のレコードを取得し、Linq を使って結合した方がよさそうです。
6
6
 
7
7
  左外部結合の実行
8
- [https://docs.microsoft.com/ja-jp/dotnet/csharp/linq/perform-left-outer-joins](https://docs.microsoft.com/ja-jp/dotnet/csharp/linq/perform-left-outer-joins)
8
+ [https://docs.microsoft.com/ja-jp/dotnet/csharp/linq/perform-left-outer-joins](https://docs.microsoft.com/ja-jp/dotnet/csharp/linq/perform-left-outer-joins)
9
+
10
+ 【追記】
11
+
12
+ 上記案でのサンプルを書いておきます。Oracle の方は Entity Framework を使えれば簡単に、CSV の方も DataSet / DataTable を作るのよりは多分少ない労力で List<T> 型のオブジェクトを作れると思います。
13
+
14
+ ```
15
+ <%@ Page Language="C#" %>
16
+
17
+ <!DOCTYPE html>
18
+
19
+ <script runat="server">
20
+
21
+ public class Employee
22
+ {
23
+ public string Id { get; set; }
24
+ public string Dept { get; set; }
25
+ public string Name { get; set; }
26
+ }
27
+
28
+ public class Role
29
+ {
30
+ public string Id { get; set; }
31
+ public string Right { get; set; }
32
+ }
33
+
34
+ public class JoinedTable
35
+ {
36
+ public string Id { get; set; }
37
+ public string Dept { get; set; }
38
+ public string Name { get; set; }
39
+ public string Right { get; set; }
40
+ }
41
+
42
+ protected void Page_Load(object sender, EventArgs erg)
43
+ {
44
+ if (!IsPostBack)
45
+ {
46
+ List<Employee> employees = new List<Employee>()
47
+ {
48
+ new Employee() { Id = "ID0001", Dept = "部署A", Name = "鈴木太郎" },
49
+ new Employee() { Id = "ID0002", Dept = "部署A", Name = "山田一郎" },
50
+ new Employee() { Id = "ID0003", Dept = "部署B", Name = "佐藤花子" }
51
+ };
52
+
53
+ List<Role> roles = new List<Role>()
54
+ {
55
+ new Role() { Id = "ID0001", Right ="〇" },
56
+ new Role() { Id = "ID0003", Right ="×" },
57
+ new Role() { Id = "ID0004", Right ="〇" }
58
+ };
59
+
60
+ var query = from e in employees
61
+ join r in roles on e.Id equals r.Id into gj
62
+ from sub in gj.DefaultIfEmpty()
63
+ select new JoinedTable
64
+ {
65
+ Id = e.Id,
66
+ Dept = e.Dept,
67
+ Name = e.Name,
68
+ Right = sub?.Right ?? string.Empty
69
+ };
70
+
71
+ GridView1.DataSource = query;
72
+ GridView1.DataBind();
73
+ }
74
+ }
75
+ </script>
76
+
77
+ <html xmlns="http://www.w3.org/1999/xhtml">
78
+ <head runat="server">
79
+ <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
80
+ <title></title>
81
+ </head>
82
+ <body>
83
+ <form id="form1" runat="server">
84
+ <div>
85
+ <asp:GridView ID="GridView1" runat="server"></asp:GridView>
86
+ </div>
87
+ </form>
88
+ </body>
89
+ </html>
90
+ ```