回答編集履歴

1

追記

2018/11/17 13:01

投稿

YAmaGNZ
YAmaGNZ

スコア10268

test CHANGED
@@ -1 +1,141 @@
1
1
  [DataTable.Merge](https://docs.microsoft.com/ja-jp/dotnet/api/system.data.datatable.merge?view=netframework-4.7.2)ってことですかね?
2
+
3
+
4
+
5
+ ### 追記
6
+
7
+
8
+
9
+ とりあえず書いてみました。
10
+
11
+ ```C#
12
+
13
+ static void Main(string[] args)
14
+
15
+ {
16
+
17
+ DataTable tableA = new DataTable();
18
+
19
+ DataTable tableB;
20
+
21
+
22
+
23
+ tableA.Columns.Add("ColumnA");
24
+
25
+ tableA.Columns.Add("ColumnB");
26
+
27
+ tableA.Columns.Add("ColumnC");
28
+
29
+ tableA.Columns.Add("ColumnD");
30
+
31
+
32
+
33
+ tableB = tableA.Clone();
34
+
35
+
36
+
37
+ //TableAのデータ作成
38
+
39
+ tableA.Rows.Add(100, "AAA", "abc", 500);
40
+
41
+ tableA.Rows.Add(100, "AAA", "abc", 600);
42
+
43
+ tableA.Rows.Add(101, "BBB", "bbb", 200);
44
+
45
+ tableA.Rows.Add(102, "CCC", "ccc", 1000);
46
+
47
+ tableA.Rows.Add(106, "FFF", "fff", 400);
48
+
49
+
50
+
51
+ //TableBのデータ作成
52
+
53
+ tableB.Rows.Add(100, "AAA", "abc", 500); //同じレコード
54
+
55
+ tableB.Rows.Add(100, "AAA", "abc", 700); //一部のカラムの値が異なる
56
+
57
+ tableB.Rows.Add(105, "EEE", "eee", 2000); //全部違う
58
+
59
+
60
+
61
+ //LINQ
62
+
63
+ var rows = tableA.AsEnumerable().Union(tableB.AsEnumerable(), DataRowComparer<DataRow>.Default);
64
+
65
+ //DataRowの塊なので、とりあえずDataTableに入れる
66
+
67
+ DataTable tableC = tableA.Clone();
68
+
69
+ foreach (DataRow r in rows)
70
+
71
+ {
72
+
73
+ tableC.ImportRow(r);
74
+
75
+ }
76
+
77
+ ViewTable(tableC);
78
+
79
+
80
+
81
+ Console.WriteLine('\n');
82
+
83
+
84
+
85
+ //Selectで
86
+
87
+ tableA.CaseSensitive = true;
88
+
89
+ foreach (DataRow r in tableB.Rows)
90
+
91
+ {
92
+
93
+ string where = $"ColumnA = {r["ColumnA"]} AND ColumnB = '{r["ColumnB"]}' AND ColumnC = '{r["ColumnC"]}' AND ColumnD = {r["ColumnD"]}";
94
+
95
+
96
+
97
+ if (tableA.Select(where).Length == 0) tableA.ImportRow(r);
98
+
99
+ }
100
+
101
+ ViewTable(tableA);
102
+
103
+
104
+
105
+
106
+
107
+
108
+
109
+ Console.ReadKey();
110
+
111
+ }
112
+
113
+
114
+
115
+ static void ViewTable(DataTable t)
116
+
117
+ {
118
+
119
+ foreach (DataRow r in t.Rows)
120
+
121
+ {
122
+
123
+ foreach (Object item in r.ItemArray)
124
+
125
+ {
126
+
127
+ Console.Write(item.ToString() + '\t');
128
+
129
+ }
130
+
131
+ Console.Write('\n');
132
+
133
+ }
134
+
135
+
136
+
137
+ }
138
+
139
+
140
+
141
+ ```