Applications Google
Menu principal

Post a Comment On: Developer Tip of the Day

"Read CSV with LinqPad"

9 Comments -

1 – 9 of 9
Blogger Net Developer said...

Nice!

Recently I was looking for something quick to read excel file and found http://code.google.com/p/excellibrary/

Using it in Linqpad was as simple as
var excelQuery = new ExcelQueryFactory(@"D:\Book1.xlsx");

var rows = from x in excelQuery.Worksheet()
select x;

1:35 am

Blogger Alex said...

Thanks for the comment Net Developer. I'm often having to dive into excel, that library looks good.

6:16 pm

Blogger Unknown said...

I have problem getting error
System.Collections.IEnumerable' does not contain a definition for 'Skip' and no extension method 'Skip' accepting a first argument of type 'System.Collections.IEnumerable' could be found (press F4 to add a using directive or assembly reference)
What should I do

10:57 am

Blogger Unknown said...

If you work with csv frequently, I would recommend this as an extension instead:
public static IEnumerable<List<String>> ReadCsv(string file)
{
string line;
using(StreamReader reader = File.OpenText(file))
{
while((line = reader.ReadLine()) != null)
{
List<String> currentLine = new List<String>(line.Split(','));
yield return currentLine;
}
}
}

This will return each line of a csv as a List<String>. You can then use linq on each line to create a strongly typed object (with property names Col1, Col2, Col3) like this:
var results = from i in MyExtensions.ReadCsv(@".\FILENAME.csv") select new {Col1 = i[0], Col2 = i[1], Col3 = i[2]};

7:44 pm

Blogger GZ said...

System.Collections.IEnumerable' does not contain a definition for 'Skip' ...

Use cast
from row in MyExtensions.ReadFrom(@"c:\sample.csv").Cast().Skip(1)

7:53 pm

Comment deleted

This comment has been removed by the author.

7:53 pm

Comment deleted

This comment has been removed by the author.

7:59 pm

Blogger GZ said...


Cast<string>()

8:02 pm

Blogger Unknown said...

Thank you Daniel Leonard!

10:40 pm

You can use some HTML tags, such as <b>, <i>, <a>

This blog does not allow anonymous comments.

Comment moderation has been enabled. All comments must be approved by the blog author.

You will be asked to sign in after submitting your comment.
Please prove you're not a robot