Login    
 
 
 
 
Text/HTML
  
You are here :: Blogs Saturday, May 19, 2012

Search
Note: This uses the internal blog search engine. The Google search engine is also available at the top of the page.
  
Disclaimer

Please review the site disclaimer before downloading or using content found on this site

  
Categories
  
DEVSHED Blog
As always, I welcome your comments!
Feb 22

Written by: Steve Gray
2/22/2010 3:45 PM  RssIcon

Let me tell you, this was a pain. It took me hours to figure this out.

This is a ground breaking piece of code. It uses a regex to correctly split a line of data – embedded commas are handled.

 

Here is how to read a .CSV file using Linq:

 Dim lines As String() = System.IO.File.ReadAllLines(strCustomerFile)
 
Dim pattern As String = ",(?=(?:[^""]*""[^""]*"")*(?![^""]*""))"
Dim r As System.Text.RegularExpressions.Regex = New System.Text.RegularExpressions.Regex(pattern)
 
Dim custs = From line In lines _
            Let data = r.Split(line) _
                Select New With {.custnmbr = data(0), _
                                 .custname = data(1)}
 
 
For Each cust In custs
    strCUSTNMBR = Replace(cust.custnmbr, Chr(34), "")
    strCUSTNAME = Replace(cust.custname, Chr(34), "")
 
Next

Tags:
Categories:

2 comment(s) so far...


Gravatar

Re: Reading a CSV file using Linq

Damn thank you soo much i been tryin to get this sorted out for awhile LOL I am in learning phase soo believe me its better to spend hours coding than tryin to figure how what the codes are doing ahahaha

Anyways thank you soooo much for putting it together :)

By Megatron on   5/31/2010 7:50 AM
Gravatar

Re: Reading a CSV file using Linq

Grats on creating this excellent regex!

By Josh on   8/25/2010 3:50 AM
As always, I welcome your comments!
  
 
 
Home | Products | Blogs | Contact Us | Links | God's Plan
Privacy Statement | Terms Of Use
 
Copyright 2011 by Devshed.us