FileHelpers and CSV: what to do when a record can expand unbounded, horizontally

后端 未结 2 1327
Happy的楠姐
Happy的楠姐 2021-01-02 09:16

I\'m trying to parse this type of CSV file with FileHelpers:

Tom,1,2,3,4,5,6,7,8,9,10
Steve,1,2,3
Bob,1,2,3,4,5,6
Cthulhu,1,2,3,4,5
Greg,1,2,3,4,5,6,7,8,9,10         


        
相关标签:
2条回答
  • 2021-01-02 09:44

    You can use an Array Field and the library will do the work:

    [DelimitedRecord(",")]
    public class MyRecord
    {
        public string Name;
    
        public int[] Values;
    }
    

    You can even use [FieldArrayLength(2, 8)]

    [DelimitedRecord(",")]
    public class MyRecord
    {
        public string Name;
    
        [FieldArrayLength(2, 8)]
        public int[] Values;
    }
    

    The set the min/max number of values

    I strongly recomend to download the last version of the library from here:

    http://teamcity.codebetter.com/viewType.html?buildTypeId=bt65&tab=buildTypeStatusDiv

    Check the artifacts section

    0 讨论(0)
  • 2021-01-02 09:47

    You could create a class MyRecord that holds all the potential values, e.g.

    [DelimitedRecord(",")]
    public class MyRecord
    {
        public string Name;
        public int Value1;
        .....
        [FieldOptional]
        public int Value5;
        ......
        [FieldOptional]
        [FieldNullValue(typeof(int), "-1" )]
        public int Value14;
    }
    

    and make most of those fields optional (no. 5 through 14 in my example) and combine that with e.g. a FieldNullValue to handle those non-existing fields (or make those optional fields a nullable int:

     public int? Value5
    
    0 讨论(0)
提交回复
热议问题