使用LINQ C#2010查询CSV [英] Query CSV using LINQ C# 2010

查看:247
本文介绍了使用LINQ C#2010查询CSV的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我按照这里的教程从CSV文件中查询内容:
http://rudesyle.wordpress.com/2008/01/28/using-linq-to-query-a-csv-file/

I follow the tutorial here to query content from CSV file: http://rudesyle.wordpress.com/2008/01/28/using-linq-to-query-a-csv-file/

但是,结果错过了第一行是
Mets,New York,NL

However, the result misses the first row which is "Mets","New York","NL"

经过一些测试,我意识到,如果我有一个空行在文件的顶部,结果是预期的。

After some testing, I realize that if I have a empty line at the top of the file, the result is as expected.

Bellow是我的代码和.csv文件

Bellow is my code and .csv file

teams.csv

"Mets","New York","NL"
"Marlins","Florida","NL"
"Orioles","Baltimore","AL"
"Pirates","Pittsburgh","NL"
"Phillies","Philadelphia","NL"

计划:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.OleDb;
using System.Data;

namespace CSVQuery
{
    class Program
    {
        static void QueryCsv()
        {

            OleDbConnection cn = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\;Extended Properties='Text;HDR=No;FMT=Delimited'");
            OleDbCommand cmd = new OleDbCommand(@"SELECT * FROM ..\..\teams.csv", cn);
            OleDbDataAdapter da = new OleDbDataAdapter(cmd);

            cn.Open();

            DataTable dt = new DataTable();
            da.Fill(dt);
            var teams = from r in dt.AsEnumerable() where r.Field<string>(2) == "NL"
                            select new { City = r.Field<string>(0),
                            TeamName = r.Field<string>(1) };

            foreach (var team in teams)
            {
                Console.WriteLine(String.Format("The {0} {1}", team.TeamName, team.City));
            }

            Console.ReadLine();

            cn.Close();

        }
        static void Main(string[] args)
        {
            CSVQuery.Program.QueryCsv();
        }
    }
}

实际结果:

The Florida Marlins
The Pittsburgh Pirates
The Philadelphia Phillies

预期结果:

The New York Mets
The Florida Marlins
The Pittsburgh Pirates
The Philadelphia Phillies

是为什么我不能查询teams.csv文件的第一行? (我需要在顶部添加一个空行,以便我可以收到预期的结果)。

The question is that why I can't query the first line of the teams.csv file? (I need to add 1 empty line at the top so that I can receive expected result).

推荐答案

=http://stackoverflow.com/questions/4597639/reading-csv-file-with-oledb-ignores-first-line-even-with-hdr-no-in-connection-str>使用OLEDB读取CSV文件即使在连接字符串中使用HDR = No也忽略第一行建议问题是因为完整路径在选择字符串中。尝试将路径放在连接字符串中,并将文件名放在Select字符串中。我不能想象为什么这应该有所作为,但那是在那里建议的。

This post Reading CSV file with OLEDB ignores first line even with HDR=No in Connection String suggests that the problem is because the full path is in the Select string. Try putting the path in the connection string and just put the file name in the Select string. I cannot imagine why this should make a difference, but that is what was suggested over there.

这篇关于使用LINQ C#2010查询CSV的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆