-1
Boas, I am currently developing an app (console app) to read data from a file in CSV format using C#. Right now I’m able to run all the fields without any kind of problem and show all the data. Anyway I’m having trouble reading more specific fields from the file you can see in the following image. At this moment my intention is to make a call to fields using conditional expressions. Examples of what I want the application to return include: All customers between 20 and 30 years. All vehicles registered before 1 January 2010. All vehicles with the enginesize 1100.
Here is the current code:
public static void ReadRecord()
{
Console.WriteLine(string.Join(" ", readRecord ("1190", @"C:\CustomerInformation.csv", 9)));
Console.ReadLine();
}
public static void addRecord(string CustomerID, string Forename, string DateOfBirth, string filepath)
{
try
{
using (System.IO.StreamWriter file = new System.IO.StreamWriter(filepath, true))
{
file.WriteLine(CustomerID + ", " + Forename + ", " + DateOfBirth);
}
}
catch (Exception ex)
{
throw new ApplicationException("This program did an oopsie :", ex);
}
}
public static string[] readRecord(string searchTerm, string filepath, int positionOfSearchTerm)
{
positionOfSearchTerm--;
string[] recordNotFound = { "Record not found" };
try
{
string[] lines = System.IO.File.ReadAllLines(@filepath);
for (int i = 0; i < lines.Length; i++)
{
string[] fields = lines[i].Split(',');
if (recordMatches(searchTerm, fields, positionOfSearchTerm))
{
Console.WriteLine("Record found");
return fields;
}
}
return recordNotFound;
}
catch (Exception ex)
{
Console.WriteLine("Unknown Error");
return recordNotFound;
throw new ApplicationException("Unknown Error:", ex);
}
}
public static bool recordMatches(string searchTerm, string[] record, int positionOfSearchTerm)
{
if (record[positionOfSearchTerm].Equals(searchTerm))
{
return true;
}
return false;
}
Of course the application only returns a simple value, I have looked for some techniques and ways to be able to return multiple values, but without great success and would appreciate some help if it were possible.
There are several things to improve there, I can try to write an example for you, but I would need a template of this CSV file, you can upload one with 10 lines at most?
– Jéf Bueno
Yes, of course. You can download the file here: https://gofile.io/? c=udpk4D Thanks for the help.
– Rui
Please don’t alter your question to circumvent the system. So you invalidate my answer and, with it, throw away the effort I had to answer it. All right you remove the accept, but don’t change the context of your question...
– Jéf Bueno
I am sorry, that was not the intention but rather to ask the question in a way that was more understandable in order to avoid repeaters, since the aim of both topics was the same. Greetings.
– Rui
Did the answer meet the request? Need to be added some more detail?
– Jéf Bueno
Unfortunately no. The program was far from the required standards and it was not possible to provide the required data within the application itself.
– Rui