2つのコレクションがあるとします。
Base Collection
![image.png](https://qiita-user-contents.imgix.net/https%3A%2F%2Fqiita-image-store.s3.ap-northeast-1.amazonaws.com%2F0%2F186221%2F0e431d24-5065-eea8-6b58-a970d29e9885.png?ixlib=rb-1.2.2&auto=format&gif-q=60&q=75&s=aa799e0055f59e7507dda67c1d8162e6)
Target Collection
![image.png](https://qiita-user-contents.imgix.net/https%3A%2F%2Fqiita-image-store.s3.ap-northeast-1.amazonaws.com%2F0%2F186221%2F91247b43-8890-d108-be93-da9139d97894.png?ixlib=rb-1.2.2&auto=format&gif-q=60&q=75&s=350cec9ad94b9804f2ba7c619cbd7682)
- フィールド1 ⇔ Field1
- フィールド2 ⇔ Field2
- フィールド3 ⇔ Field3
に対応しているとします。
2つのコレクションを比較して、上記のフィールドで同じパラメータのレコードがあった場合にレコード削除&差分抽出するオブジェクトを作りました。
概要図
![image.png](https://qiita-user-contents.imgix.net/https%3A%2F%2Fqiita-image-store.s3.ap-northeast-1.amazonaws.com%2F0%2F186221%2Ff06892c6-3818-138c-58b7-ffd2cf53839c.png?ixlib=rb-1.2.2&auto=format&gif-q=60&q=75&s=bdbd7bf80836395c7fe01f858f94f2b6)
実行結果
比較元と比較対象のコレクションの差分抽出、重複レコードを削除した結果は以下の通りです。
Base Collectionのフィールド1~3を対象とし、対応するTarget CollectionのField1~3を比較した結果です。
Result Base Collection
Base CollectionとTarget Collectionで重複しなかったレコードを抽出したコレクション。
抽出元のコレクションはBase Colelctionです。
![image.png](https://qiita-user-contents.imgix.net/https%3A%2F%2Fqiita-image-store.s3.ap-northeast-1.amazonaws.com%2F0%2F186221%2F113afde7-8923-1abc-1f9f-7aa6d8964e54.png?ixlib=rb-1.2.2&auto=format&gif-q=60&q=75&s=3af6cd29eb6c02771b7be380764dd7a5)
Result Target Collection
Base CollectionとTarget Collectionで重複しなかったレコードを抽出したコレクション。
抽出元のコレクションはTarget Colelctionです。
![image.png](https://qiita-user-contents.imgix.net/https%3A%2F%2Fqiita-image-store.s3.ap-northeast-1.amazonaws.com%2F0%2F186221%2F3e317c60-0686-5734-2cb2-78f79fa71e25.png?ixlib=rb-1.2.2&auto=format&gif-q=60&q=75&s=e69a804481660c1aadade5983daa6939)
Duplicate Collection
Base CollectionとTarget Collectionで重複したレコードを抽出したコレクション。
抽出元のコレクションはTarget Colelctionです。
![image.png](https://qiita-user-contents.imgix.net/https%3A%2F%2Fqiita-image-store.s3.ap-northeast-1.amazonaws.com%2F0%2F186221%2Fe8d6ea57-127f-4bf3-3a0a-138c9b291677.png?ixlib=rb-1.2.2&auto=format&gif-q=60&q=75&s=d968e37fa472da9f407430193df63ba6)
オブジェクト説明
オブジェクト全体フロー
![image.png](https://qiita-user-contents.imgix.net/https%3A%2F%2Fqiita-image-store.s3.ap-northeast-1.amazonaws.com%2F0%2F186221%2F7564d342-8193-ebe2-0079-38ac110b619a.png?ixlib=rb-1.2.2&auto=format&gif-q=60&q=75&s=9be0241b3ab8c12349dd791ee8e46f8a)
入力
Base FieldとTarget Fieldが対応するように、フィールド名を設定します。
Base Field1とTarget Field1は入力必須です。
![image.png](https://qiita-user-contents.imgix.net/https%3A%2F%2Fqiita-image-store.s3.ap-northeast-1.amazonaws.com%2F0%2F186221%2F145becff-1ae7-539d-76b4-46116efa008e.png?ixlib=rb-1.2.2&auto=format&gif-q=60&q=75&s=938ab83e5639c211a0999052bad1fd2c)
出力
![](https://falcslab.com/wp-content/uploads/2020/05/image-35.png)
ソースコード
Base Collectionのレコード重複を除外した後、そのCollectionをベースにして
Target Collectionとの対応するフィールドと比較して差分抽出、重複削除を行っています。
Try
If Base_Field1 = "" Or Target_Field1 = "" Then
Success = False
Message = "Base_Field1, Target_Field1は必須項目です。"
Exit Sub
End If
Dim NewBase_Collection As DataTable
NewBase_Collection = Base_Collection.Clone
Dim count As Long = 0
For Each r1 As DataRow In Base_Collection.Rows
If count = 0 Then
NewBase_Collection.ImportRow(r1)
Else
Dim find_rows As DataRow()
find_rows = NewBase_Collection.Select(Base_Field1 & " = '" & r1(Base_Field1) & "'" & _
If(Base_Field2 <> "", " AND " & Base_Field2 & " = '" & r1(Base_Field2) & "'", "") & _
If(Base_Field3 <> "", " AND " & Base_Field3 & " = '" & r1(Base_Field3) & "'", ""))
If find_rows.Length = 0 Then
NewBase_Collection.ImportRow(r1)
End If
End If
count = count + 1
Next
Dim Tmp_Duplicate_Collection, Tmp_Result_Target_Collection, Tmp_NewBase_Collection As DataTable
Tmp_Duplicate_Collection = Target_Collection.Clone
Tmp_Result_Target_Collection = Target_Collection.copy()
Tmp_NewBase_Collection = NewBase_Collection.Clone
For Each r2 As DataRow In NewBase_Collection.Rows
Dim find_rows As DataRow()
find_rows = Tmp_Result_Target_Collection.Select(Target_Field1 & " = '" & r2(Base_Field1) & "'" & _
If(Target_Field2 <> "", " AND " & Target_Field2 & " = '" & r2(Base_Field2) & "'", "") & _
If(Target_Field3 <> "", " AND " & Target_Field3 & " = '" & r2(Base_Field3) & "'", ""))
If find_rows.Length <> 0 Then
' Base_CollectionにもTarget_Collectionにも存在する場合
For Each r3 As DataRow In find_rows
Tmp_Duplicate_Collection.ImportRow(r3)
r3.Delete()
Next
Else
Tmp_NewBase_Collection.ImportRow(r2)
End If
Next
Duplicate_Collection = Tmp_Duplicate_Collection.DefaultView.ToTable
Result_Target_Collection = Tmp_Result_Target_Collection.DefaultView.ToTable
Result_Base_Collection = Tmp_NewBase_Collection.DefaultView.ToTable
Success = True
Message = ""
Catch e As Exception
Success = False
Message = e.Message
End Try
サンプル
https://github.com/falcslab/blueprism/tree/collection
BPA オブジェクト – コレクション差分比較.xml