Querying Data from CSV File in C# running in the console RRS feed

  • Question

  • I am struggling to find good information how to query data from a CSV file.

    I have to query the CSV file to generate reports and then run in the console.

    Can anyone help?

    These are the fields that I am using below. 

    Customer Information

    • Forename
    • Surname
    • Date of Birth

    Vehicle Information

    • Manufacturer
    • Model
    • Registration number
    • Registration date
    • Engine size (in cc)
    • Owner
    • InteriorColour (Car only)
    • Has Helmet Storage (Motorcycle only)

    Required Relationships

    • Customers can have 1 to many vehicles.
    • Vehicle must have exactly one owner.
    • Vehicle type cannot be changed once it is created.


    • We will require reports to be designed to contain:
      • All known customers and any vehicles they own.
      • All customers between the age of 20 and 30.
      • All Vehicles registered before 1<sup>st</sup> January 2010.
      • All Vehicles with an engine size over 1100.

    Wednesday, March 14, 2018 10:54 PM

All replies

  • Hello,

     Query may not work as expected on CSV files. CSV files are not structured

    but formatted where each element is Comma Seperated Values.  Depending

    on the format, the CSV may or may not have a Header that identifies the

    Column Names for use in DataTables objects.


     From looks of your criteria, its some sort of class assignment.  You are there

    to learn not for us to give code away.


     However, I will give you some suggestions as a guide;


     1. Define Vehicle as Class objects.

     2. Define Customer Class object that also inherients Vehicle class.

     3. Use Customer properties SurName as the accessor in List<> or

         Dictionary<>/Hash<> container objects when unique.

     4. Declare Customer instance and read CSV data file.

     5. Remember when reading Vehicle data you will make sure that

         the proper Customer object is used.


     If you plan to use DataTables then your job will be way more easy

    but require more code to populate and maintain entries. Code for

    Queries is another step but need structured data first.


     Because we do not have a workable CSV sample file, most of the

    suggestions will be basic steps. The more details we have then

    the more accuarate of a solution is provided.  Except for homework,

    we prefer to see what you have and help you to the next step.


     Hope this helps :)

    Thursday, March 15, 2018 12:43 AM
  • 2. Define Customer Class object that also inherients Vehicle class.

    Everything you say sounds good to me except you might have been too hasty about saying that Customers should inherit Vehicles. I think a customer should embed (contain) a list or array of zero or more vehicles. Is that what you intended?

    Sam Hobbs

    Thursday, March 15, 2018 5:07 AM
  • Hello,

     Perhaps that's true but the OP also wanted reverse lookup by Vehicle

    in the generated Reports functionality. Which made me think that the

    Vehicle Class is outside the scope of Customer Class. If not then every

    Query will access Vehicle object via Customer Class... Don't you think???

     Thanks :)

    Thursday, March 15, 2018 7:12 AM
  • I would start with the TextFieldParser to read the data from the CSV file. That should be the easy part. From there you can add the data to a DataSet and/or DataTables in a DataSet if you want to break the information down into separate related DataTables.

    Paul ~~~~ Microsoft MVP (Visual Basic)

    Thursday, March 15, 2018 2:32 PM
  • Hi, Have you found your solution? or are you still having trouble completing this?? Please let me know.
    Monday, May 28, 2018 12:07 AM