如何为迭代字段创建客户类使用FileHelper库

时间:2013-09-02 12:39:42

标签: c# filehelpers

我正在使用优秀的FileHelpers library。我对Customer类的迭代字段有疑问。 如下所示,L1~L51只是数据字段。

[DelimitedRecord(",")]
public class Customer
{
    public string Time;
    public double L1;
    public double L2;
    public double L3;
    public double L4;
    public double L5;
    public double L6;
    public double L7;
    public double L8;
    public double L9;
    public double L10;
    public double L11;
    public double L12;
    public double L13;
    public double L14;
    public double L15;
    public double L16;
    public double L17;
    public double L18;
    public double L19;
    public double L20;
    public double L21;
    public double L22;
    public double L23;
    public double L24;
    public double L25;
    public double L26;
    public double L27;
    public double L28;
    public double L29;
    public double L30;
    public double L31;
    public double L32;
    public double L33;
    public double L34;
    public double L35;
    public double L36;
    public double L37;
    public double L38;
    public double L39;
    public double L40;
    public double L41;
    public double L42;
    public double L43;
    public double L44;
    public double L45;
    public double L46;
    public double L47;
    public double L48;
    public double L49;
    public double L50;
    public double L51;
    public string last;
}

EDITED 数据格式只是:我有第一行的标题。我如何建立客户类?

    Time,L1,L2,L3,L4,L5,L6,L7,L8,L9,L10,L11,L12,L13,L14,L15,L16,L17,L18,L19,L20,L21,L22,L23,L24,L25,L26,L27,L28,L29,L30,L31,L32,L33,L34,L35,L36,L37,L38,L39,L40,L41,L42,L43,L44,L45,L46,L47,L48,L49,L50,L51,
2013-08-29 오후 4:41:21,12.817,34.447,14.302,74.672,76.082,2.766,27.258,10.532,21.266,3.546,68.156,8.31,19.472,7.199,51.964,6.975,44.893,55.766,134.145,145.979,16.246,74.095,99.121,49.657,22.815,14.174,44.615,61.78,48.514,52.316,-1.357,8.641,10.265,15.562,47.328,12.561,14.676,13.234,55.211,29.533,49.059,42.693,52.754,14.9,71.916,61,94.955,82.832,47.04,39.178,66.191,
    2013-08-29 오후 4:41:21,12.817,34.447,14.302,74.672,76.082,2.766,27.258,10.532,21.266,3.546,68.156,8.31,19.472,7.199,51.964,6.975,44.893,55.766,134.145,145.979,16.246,74.095,99.121,49.657,22.815,14.174,44.615,61.78,48.514,52.316,0,8.641,10.265,0,0,0,14.676,13.234,55.211,29.533,49.059,42.693,0,0,0,0,0,0,0,39.178,0,
    2013-08-29 오후 4:41:21,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,44.615,61.78,48.514,52.316,0,8.641,10.265,0,0,0,14.676,13.234,55.211,29.533,49.059,42.693,0,0,0,0,0,0,0,39.178,0,
    2013-08-29 오후 4:41:22,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,

可以写下面的内容吗?或任何建议?

[DelimitedRecord(",")]
public class Customer
{
    public string Time;
    public double[] L;
}

1 个答案:

答案 0 :(得分:2)

可以使用FieldArrayLength属性:

[DelimitedRecord(",")]
public class Customer
{
    public string Time;

    [FieldArrayLength(51)]
    public double[] L;

    public string last;
}

由于数据中的最后一列(似乎始终为空),您必须提供FieldArrayLength属性。请注意,如果您对该字段感到困扰,可以将last设为私有而非公开。

另一种可能的解决方案是使用double?代替double,但是您必须处理尾随的null值。

[DelimitedRecord(",")]
public class Customer
{
    public string Time;

    public double?[] L;
}

第三种解决方案是使用FieldNullValue属性提供最后一列中默认数据的值,例如:

[DelimitedRecord(",")]
public class Customer
{
    public string Time;

    [FieldNullValue(Double.NaN)] 
    public double[] L;
}

修改:要跳过文件的第一行,请使用IgnoreFirst属性:

[DelimitedRecord(",")]
[IgnoreFirst(1)]
public class Customer
{
    public string Time;

    [FieldArrayLength(51)]
    public double[] L;

    public string last;
}