从控制台中运行的C#中查询CSV文件中的数据 [英] Querying data from CSV file in C# running in the console

查看:167
本文介绍了从控制台中运行的C#中查询CSV文件中的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力寻找如何从CSV文件中查询数据的良好信息。



我必须查询CSV文件以生成报告然后在控制台。



有人可以帮忙吗?



这些是我在下面使用的字段。



客户信息



Forename

姓氏

出生日期



车辆信息



制造商

型号

注册号

注册日期

发动机尺寸(单位为cc)



所有者



InteriorColour(仅限汽车)

有头盔存放(仅限摩托车)

所需关系

客户可以拥有1到多辆车。

车辆必须只有一个车主。

车辆类型一旦创建就无法更改。



报告



我们将要求报告包含:

所有已知客户和他们拥有的任何车辆。

所有20至30岁之间的客户。

所有车辆在2010年1月 st 之前登记。

所有发动机尺寸超过1100的车辆。



Wh在我尝试过:



在控制台中显示CSV文件



I am struggling to find good information how to query data from a CSV file.

I have to query the CSV file to generate reports and then run in the console.

Can anyone help?

These are the fields that I am using below.

Customer Information

Forename
Surname
Date of Birth

Vehicle Information

Manufacturer
Model
Registration number
Registration date
Engine size (in cc)

Owner

InteriorColour (Car only)
Has Helmet Storage (Motorcycle only)
Required Relationships
Customers can have 1 to many vehicles.
Vehicle must have exactly one owner.
Vehicle type cannot be changed once it is created.

Reports

We will require reports to be designed to contain:
All known customers and any vehicles they own.
All customers between the age of 20 and 30.
All Vehicles registered before 1st January 2010.
All Vehicles with an engine size over 1100.

What I have tried:

Displaying the CSV file in the console

using System;
using System.IO;

namespace PinewoodTest
{
    class Class1
    {
        static void Main(string[] args)
        {
            try
            {
                String st = File.ReadAllText("/Users/thomasjeffcock/Projects/PinewoodTest/CustomerInformation.csv");
                Console.WriteLine(st);
            }
            catch(Exception e)
            {
                Console.WriteLine("The File Could Not Be Read");
                Console.WriteLine(e.Message);
            } 

            Console.Read();
        }
    }
}

推荐答案

我建​​议使用CSV帮助器(通过NuGet添加它)您的项目的数据包管理器 GitHub - JoshClose / CsvHelper:帮助读取和写入CSV文件的库 [ ^ ]从文件中读取数据。创建可查询对象的集合(例如List)(定义你的类型)然后使用Linq查找,排序和排序信息。
I would recommend using CSV helper (add it via NuGet packet manager to your project GitHub - JoshClose/CsvHelper: Library to help reading and writing CSV files[^] to read data from file. Create collection (List for example) of queryable objects (define your type) and then use Linq to find, sort and order the information.


参见使用OleDb导入文本文件(选项卡,CSV,自定义) [ ^ ]。但是,作为 PIEBALDconsult - Pro fessional Profile [ ^ ]建议使用ACE引擎,您可以从下载Microsoft Access数据库免费下载引擎2016可从Microsoft官方下载中心重新分发 [ ^ ]。
See Using OleDb to Import Text Files (tab, CSV, custom)[^]. But, as PIEBALDconsult - Professional Profile[^] suggests above, use the ACE engine, which you can download free from Download Microsoft Access Database Engine 2016 Redistributable from Official Microsoft Download Center[^].


这篇关于从控制台中运行的C#中查询CSV文件中的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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