I have a process that parses a rather ugly text file (lots of them actually), and populates records into a list of a custom class set up with the names of the fields that I will sql bulk copy into a table after converting it into a data table. It works GREAT, however after reviewing the data it appears we have a new requirement to count duplicate records. These records indicate actions, and it is OK to have duplicates, unfortunately each duplicate record is a genuine duplicate, nothing separating it at all from the other, so instead of listing the same record twice (or more), I want to indicate how many times it occurred. This will also allow me to set up a primary key on the table as there will not be duplicates to contend with.
As I read the lines of the file, I stop with a footer record and populate my class record with the data needed and insert it into the list of that class. When the file is done processing, the completed list of class records is then converted to a data table.
My question is this: is there a simple way populate the field "Count" in my class with the number of times the record appears, either after the list is complete, or after converted to datatable?
I have something like this:
As I read the lines of the file, I stop with a footer record and populate my class record with the data needed and insert it into the list of that class. When the file is done processing, the completed list of class records is then converted to a data table.
My question is this: is there a simple way populate the field "Count" in my class with the number of times the record appears, either after the list is complete, or after converted to datatable?
I have something like this:
List<MyRecord> records = new List<MyRecord>(); var read = File.ReadAllLines(FILE); var lines = new List<string>(read); foreach (string line in lines) { if (line.Contains("ENDREC")) { records.add(new MyRecord(Data1, Data2, Data3, "1")); //the 1 would be the count, defaulting to 1 } } DataTable table = ConvertToDataTable(records); //uses a function I wrote to convert the list to a datatable public class MyRecord { public string Data1 { get; set; } public string Data2 { get; set; } public string Data3 { get; set; } public int Count { get; set; } //constructor here }