回答編集履歴
1
追記
answer
CHANGED
@@ -1,1 +1,71 @@
|
|
1
|
-
[DataTable.Merge](https://docs.microsoft.com/ja-jp/dotnet/api/system.data.datatable.merge?view=netframework-4.7.2)ってことですかね?
|
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
|
+
```C#
|
7
|
+
static void Main(string[] args)
|
8
|
+
{
|
9
|
+
DataTable tableA = new DataTable();
|
10
|
+
DataTable tableB;
|
11
|
+
|
12
|
+
tableA.Columns.Add("ColumnA");
|
13
|
+
tableA.Columns.Add("ColumnB");
|
14
|
+
tableA.Columns.Add("ColumnC");
|
15
|
+
tableA.Columns.Add("ColumnD");
|
16
|
+
|
17
|
+
tableB = tableA.Clone();
|
18
|
+
|
19
|
+
//TableAのデータ作成
|
20
|
+
tableA.Rows.Add(100, "AAA", "abc", 500);
|
21
|
+
tableA.Rows.Add(100, "AAA", "abc", 600);
|
22
|
+
tableA.Rows.Add(101, "BBB", "bbb", 200);
|
23
|
+
tableA.Rows.Add(102, "CCC", "ccc", 1000);
|
24
|
+
tableA.Rows.Add(106, "FFF", "fff", 400);
|
25
|
+
|
26
|
+
//TableBのデータ作成
|
27
|
+
tableB.Rows.Add(100, "AAA", "abc", 500); //同じレコード
|
28
|
+
tableB.Rows.Add(100, "AAA", "abc", 700); //一部のカラムの値が異なる
|
29
|
+
tableB.Rows.Add(105, "EEE", "eee", 2000); //全部違う
|
30
|
+
|
31
|
+
//LINQ
|
32
|
+
var rows = tableA.AsEnumerable().Union(tableB.AsEnumerable(), DataRowComparer<DataRow>.Default);
|
33
|
+
//DataRowの塊なので、とりあえずDataTableに入れる
|
34
|
+
DataTable tableC = tableA.Clone();
|
35
|
+
foreach (DataRow r in rows)
|
36
|
+
{
|
37
|
+
tableC.ImportRow(r);
|
38
|
+
}
|
39
|
+
ViewTable(tableC);
|
40
|
+
|
41
|
+
Console.WriteLine('\n');
|
42
|
+
|
43
|
+
//Selectで
|
44
|
+
tableA.CaseSensitive = true;
|
45
|
+
foreach (DataRow r in tableB.Rows)
|
46
|
+
{
|
47
|
+
string where = $"ColumnA = {r["ColumnA"]} AND ColumnB = '{r["ColumnB"]}' AND ColumnC = '{r["ColumnC"]}' AND ColumnD = {r["ColumnD"]}";
|
48
|
+
|
49
|
+
if (tableA.Select(where).Length == 0) tableA.ImportRow(r);
|
50
|
+
}
|
51
|
+
ViewTable(tableA);
|
52
|
+
|
53
|
+
|
54
|
+
|
55
|
+
Console.ReadKey();
|
56
|
+
}
|
57
|
+
|
58
|
+
static void ViewTable(DataTable t)
|
59
|
+
{
|
60
|
+
foreach (DataRow r in t.Rows)
|
61
|
+
{
|
62
|
+
foreach (Object item in r.ItemArray)
|
63
|
+
{
|
64
|
+
Console.Write(item.ToString() + '\t');
|
65
|
+
}
|
66
|
+
Console.Write('\n');
|
67
|
+
}
|
68
|
+
|
69
|
+
}
|
70
|
+
|
71
|
+
```
|