解析CSV格式的文本文件

tpo*_*pow 7 c# parsing

我有一个看起来像这样的文本文件:

1,Smith, 249.24, 6/10/2010
2,Johnson, 1332.23, 6/11/2010
3,Woods, 2214.22, 6/11/2010
1,Smith, 219.24, 6/11/2010
Run Code Online (Sandbox Code Playgroud)

我需要能够在给定日期找到客户的余额.

我想知道我是否应该:

A.从最后开始,每行读一个数组到一个数组.检查姓氏索引以查看它是否是我们正在寻找的客户端.然后,显示第一个匹配的余额索引.

要么

B.使用RegEx查找匹配并显示它.

我对RegEx没有多少经验,但如果在这样的情况下,我会学到它.

ber*_*r2k 6

我建议使用FileHelpers opensource项目:http: //www.filehelpers.net/

小菜一碟:

定义你的课程:

[DelimitedRecord(",")]
public class Customer
{
    public int CustId;

    public string Name;

    public decimal Balance;

    [FieldConverter(ConverterKind.Date, "dd-MM-yyyy")]
    public DateTime AddedDate;

}   
Run Code Online (Sandbox Code Playgroud)

用它:

var engine = new FileHelperAsyncEngine<Customer>();

// Read
using(engine.BeginReadFile("TestIn.txt"))
{
   // The engine is IEnumerable 
   foreach(Customer cust in engine)
   {
      // your code here
      Console.WriteLine(cust.Name);

      // your condition >> add balance
   }
}
Run Code Online (Sandbox Code Playgroud)