3

I'm using excellent FileHelpers library. I have a question for iteration fields of Customer class. As you can see below class, L1 ~ L51 is just field of datas.

[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 Data Format is just :I have a title of first row. How can I build the customer class ?

    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,

Is it possible to write like below ? or any suggestions ?

[DelimitedRecord(",")]
public class Customer
{
    public string Time;
    public double[] L;
}
Changju.rhee
  • 463
  • 3
  • 11
  • 26
  • If it is not possible, just throw that library from the window, because if it forces you to have 50 fields, it is nuts! :) – Ondrej Janacek Sep 02 '13 at 12:59
  • i dont get the point in this library – string.Empty Sep 02 '13 at 13:02
  • 1
    @Nicolas, the best way to use FileHelpers is to think of the record class as just a 'spec' for your file format. You use it to describe the file using only public fields (yes this goes against C# best practice, I know, but remember it's just the spec). Once you have loaded the file into an array of MyFileHelpersClass[] which FileHelpers does very well, then you can map the values (e.g., with a foreach) to a more sensible class with properties, methods, additional functionality, etc. – shamp00 Sep 03 '13 at 12:56
  • There are other methods though. but thanks for the clarification. – string.Empty Sep 03 '13 at 12:58

1 Answers1

2

It's possible, just use the FieldArrayLength attribute:

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

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

    public string last;
}

You have to provide the FieldArrayLength attribute because of the last column in your data (which seems to be always empty). Note that you could make last private instead of public if you're bothered about the field.

Another possible solution is to use double? instead of double, but then you'll have to deal with the trailing null values.

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

    public double?[] L;
}

A third solution is to provide a value the data in the last column should default to, using the FieldNullValue attribute, e.g.:

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

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

Edit: To skip the first row of your file, use the IgnoreFirst attribute:

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

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

    public string last;
}
sloth
  • 99,095
  • 21
  • 171
  • 219