There are different ways of doing this, I will suggest one, but that does not mean it is the only way.
Since you say you can extract and export the data from the first file, then I will assume the issue is not how to open and read the files.
My suggestion is you create yourself a class representing what you want to export:
-
class Customer{
-
int customerNumber;
-
string firstName;
-
string lastName;
-
string phone;
-
DateTime lastCheckDate;
-
string shippingAddress;
-
-
public Customer(int custNumber){
-
this.customerNumber = custNumber;
-
}
-
-
public int CustomerNumber{
-
get{ return this.customerNumber;}
-
}
-
public string FirstName{
-
get{return this.firstName;}
-
set{this.firstName = value;}
-
}
-
// etc for LastName, Phone, LastCheckDate Address
-
}
-
Now in your main process:
Create a List of Customers
- List<Customer> myCustomerList = new List<Customer>();
Now when you read your first file, you extract the fields and create a customer instance for each record.
This assumes there is only one record per customer in each file. If there are duplicates, you will have some
decisions to make, but we won't complicate this initial idea with that yet.
-
// pseudocode: Open File. Read fields...
-
// while ReadLine(){
-
Customer tempCustomer = new Customer(Int32.Parse(field1));
-
tempCustomer.LastName = field2;
-
tempCustomer.FirstName = field3;
-
// etc.
-
-
// Add the Customer to your list:
-
-
myCustomerList.Add(tempCustomer);
-
-
// } end read
-
Now you have a list of customers, and two more files to read.
Essentially you are going to read the next file line for line, and for each line,
retrieve the Customer object with the matching Id from your list.
Then you will udpdate the Customer object with the fields from that record in file2.
You will do the same for file3.
There are several ways to search for an object in a list. Here is one:
Create a method which calls the
List.Find() method and returns the Customer that matches the Id you want.
This method will have to create a delegate to a method that returns true if a single Customer object has a matching Id:
-
private Customer FindByCustomerNumber(int customerNumber){
-
-
// List.Find uses a delegate to a method which returns true or false
-
Predicate<Customer> findMatchingIdDelegate =
-
delegate(Customer c) { return c.CustomerNumber == customerNumber; };
-
-
return myCustomerList.Find(findMatchingIdDelegate);
-
-
// Note: if no customer with that id is found, this method will return null, so make sure you deal with that case.
-
}
-
So, back to reading file2 and file3: you read each line (record), get the customer number, and then find the matching item in your List:
-
// while (ReadLine()){
-
Customer existingCustomer =
-
FindByCustomerNumber(Int32.Parse(currentIdField));
-
-
existingCustomer.ShippingAddress = field2;
-
-
// etc for each field in the current record of the current file
-
// } // end ReadLine()
-
Same for file3;
Now you have a list of Customer objects with all the fields you want.
Feed them into whatever export process you already have:
-
// pseudocode:
-
foreach(Customer c in myCustomerList){
-
TextWriter.WriteLine(c.CustomerNumber.ToString() + c.LastName + c.FirstName + c.ShippingAddress); // etc etc
-
}
-
In a quick and dirty world, all of the above, except your customer class, would basically go into your Main method: