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

  • Home
  • SEARCH
  • 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 6120723
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T15:42:04+00:00 2026-05-23T15:42:04+00:00

Anyone know how to (or if its possible) when using linq if one of

  • 0

Anyone know how to (or if its possible) when using linq if one of the iterations fails to just ignore the fails and keep the successful. Example: when I’m importing a CSV:

string[] csvRows = File.ReadAllLines(fiList[fileCount].FullName);      

var Rows = from row in csvRows.Skip(1)
           let data = row.Split(',')
           select new
           {
               Reference = data[0],
               RequestDate = Convert.ToDateTime(data[1]),
           };

and the csv looks like:

Reference, RequestDate ref1,20/06/10
00:00:00 ref2,55/06/10 00:00:00

Obviously, it’ll fail on the second one as it can’t convert to a datetime (“String was not recognized as a valid DateTime.”). Is there any way to force .net to ignore this and just return the first row?

Cheers in advance

Stu

  • 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-05-23T15:42:04+00:00Added an answer on May 23, 2026 at 3:42 pm

    You could test and filter out the invalid rows:

    var rows = csvRows.Skip(1).Select(r => 
        {
            var data = r.Split(',');
            DateTime result;
    
            if(DateTime.TryParse(data[1], out result))
                return new { Reference = data[0], RequestDate = result};
            else return null;
        }).Where( x=> x!=null);
    

    Personally I’d go for a foreach loop in this case which would make the code more readable.

    An alternative to support readability is to factor out the validity testing (just an example):

    public static bool IsValidColumnSet(string[] data)
    {
        DateTime result;
    
        if(DateTime.TryParse(data[1], out result))
            return true;
        else return false;
    }
    
    var Rows = from row in csvRows.Skip(1)
               let data = row.Split(',')
               where IsValidColumnSet(data)
               select new
               {
                   Reference = data[0],
                   RequestDate = Convert.ToDateTime(data[1]),
               };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Does anyone know if its possible to create a new property on an existing
Does anyone know if its possible to use phing to copy an entire folder
Does anyone know how its possible to retrieve the DNS records for a domain.
Does anyone know if its possible to adjust the MTU while sending an ICMP
Does anyone know if its possible to validate an XML schema with another XML
does anyone know if its possible to replace the updatepanel in asp.net ajax toolkit
does anyone know if its possible to serve with cloudfront over https with your
Does anyone out there know if its possible to show a working version of
Anyone know if it's possible to databind the ScaleX and ScaleY of a render
Does anyone know if it's possible to use regex capture within Apache's DirectoryMatch directive?

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.