Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8466089
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T15:14:23+00:00 2026-06-10T15:14:23+00:00

var csv = from line in File.ReadAllLines(C:/file.csv) let customerRecord = line.Split(‘,’) select new Customer()

  • 0
var csv =
    from line in File.ReadAllLines("C:/file.csv")
    let customerRecord = line.Split(',')
    select new Customer()
        {
            contactID = customerRecord[0],
            surveyDate = customerRecord[1],
            project = customerRecord[2],
            projectCode = customerRecord[3]
        };

public class Customer
    {
        public string contactID { get; set; }
        public string surveyDate { get; set; }
        public string project { get; set; }
        public string projectCode { get; set; }
    }

I’d like to be able to read the surveyDate as a DateTime in order to compare it with other DateTime fields I’m joining to the csv file.

I’ve tried just setting surveyDate as Date.Time in the class, and I’ve tried converting it in the select statement, but both fail with the following error:

FormatException: The string was not recognized as a valid DateTime. There is an unknown word starting at index 0.

I do have to work with the csv file, so I can’t add the data to sql server.

contactID,responseDate,project,projectID
1,6/4/2009,0-3 months,1
2,6/11/2009,0-3 months,1

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T15:14:25+00:00Added an answer on June 10, 2026 at 3:14 pm

    Right… Because you can never know what the input will be from the CSV you can’t always try and implicitly convert the string to a DateTime and because you want to do the convert of a string within the select a way to do this is an extension method.

      public static class StringExtensions
      {
        public static DateTime TryParseDate(this string strToParse)
        {
          DateTime dt = new DateTime();
          DateTime.TryParse(strToParse, out dt);
          return dt;
        }
      }
    

    Then when you want to convert in the select do it like this:

      var csv = from line in File.ReadAllLines("C:/file.csv")
                let customerRecord = line.Split(',')
                select new Customer()
                    {
                      contactID = customerRecord[0],
                      surveyDate = customerRecord[1].TryParseDate(),
                      project = customerRecord[2],
                      projectCode = customerRecord[3]
                    };
    

    You have your own method. Hope this helps and works for you.

    The string you want to parse will be passed to the method and then you can handle it and return a valid date time, handling incorrect inputs.

    http://msdn.microsoft.com/en-us/library/bb383977.aspx

    See this link for an explanation

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this code for parsing a CSV file. var query = from line
The task is to convert the CSV file into XML. var x = from
I'm generating a CSV file from the following code public ActionResult Index() { var
If the following regex can split a csv string by line. var lines =
I am processing CSV File Say ABC|06|001 PPP|06|001 I am running LINQ to split
I have a datagrid in a WPF populated from a csv file. This is
I have a CSV file. Each line is made up of the same format
I use next code to generate .csv file from my logs table. public ActionResult
I have a data table I've loaded from a CSV file. I need to
I have a large data coming from a csv file which looks something like

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.