-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcsvCompare.cs
More file actions
91 lines (89 loc) · 3.71 KB
/
csvCompare.cs
File metadata and controls
91 lines (89 loc) · 3.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace csvDifferences
{
class csvCompare
{
public csv a;
public csv b;
public List<int> colIndexToCompare;
public int colIndexToGroupBy;
public Dictionary<string, string> bufferFound;
public List<int> indexesNotMatched;
public List<int> indexesMatched;
public csvCompare(csv a, csv b, string[] headersToCompare, string headerToGroupBy, string pathToResult)
{
this.a = a;
this.b = b;
this.bufferFound = new Dictionary<string, string>();
this.indexesNotMatched = new List<int>();
this.indexesMatched = new List<int>();
this.colIndexToCompare = new List<int>();
string[] aHeaders = a.headers.ToArray<string>();
string[] bHeaders = b.headers.ToArray<string>();
if ((aHeaders.Length == bHeaders.Length) && (headersToCompare.Length < aHeaders.Length))
{
foreach (string header in headersToCompare)
{
if (Array.IndexOf<string>(aHeaders, header) != -1)
{
this.colIndexToCompare.Add(Array.IndexOf<string>(aHeaders, header));
}
}
if (Array.IndexOf<string>(aHeaders, headerToGroupBy) != -1)
{
this.colIndexToGroupBy = Array.IndexOf<string>(aHeaders, headerToGroupBy);
}
for (int h = 0; h < a.rows.Count; h++)
{
if (h % 100 == 0)
{
Console.WriteLine("Compared " + h + " / " + a.rows.Count);
}
List<string> row = a.rows.ElementAt(h);
List<string> cellsStart = new List<string>();
string grouping = row.ElementAt<string>(this.colIndexToGroupBy);
for (int k = 0; k < this.colIndexToCompare.Count; k++)
{
cellsStart.Add(row.ElementAt<string>(k).ToLower());
}
string[] compareA = cellsStart.ToArray<string>();
bool matched = false;
for (int i = 0; i < b.rows.Count; i++)
{
List<string> cellsEnd = new List<string>();
List<string> rowB = b.rows.ElementAt(i);
for (int j = 0; j < this.colIndexToCompare.Count; j++)
{
cellsEnd.Add(rowB.ElementAt<string>(j).ToLower());
}
string[] compareB = cellsEnd.ToArray<string>();
if (compareA.SequenceEqual(compareB))
{
matched = true;
this.indexesMatched.Add(h);
break;
}
}
if (!matched)
{
this.indexesNotMatched.Add(h);
}
}
if (this.indexesNotMatched.Count > 0)
{
List<string> rowsToWrite = new List<string>();
rowsToWrite.Add(a.getHeaderForWrite());
for (int i = 0; i < this.indexesNotMatched.Count; i++)
{
rowsToWrite.Add(a.getRowForWrite(i));
}
string[] rowsArray = rowsToWrite.ToArray<string>();
System.IO.File.WriteAllLines(pathToResult, rowsArray);
}
}
}
}
}