CsvHelper icon indicating copy to clipboard operation
CsvHelper copied to clipboard

Inheriting Parent index not working.

Open Edgaras91 opened this issue 2 years ago • 4 comments

Describe the bug

From below, writing ExtraFieldsModel to csv file, writes FullName as 1st column and Email as a 2nd column.

    public class ExtraFieldsModel : BaseModel
    {
        public string FullName { get; set; }
    }

    public class BaseModel
    {
        [Index(0)]
        public string Email { get; set; }
    }

Expected behavior Email field to be the 1st column

Workaround Add [Index(1)] to the FullName property

Edgaras91 avatar Sep 14 '21 09:09 Edgaras91

You need to specify the index on every property. The order can't be determined correctly without it.

JoshClose avatar Sep 14 '21 18:09 JoshClose

I have found that only 2 property indexes need to be specified for Email to be in 1st column. Any additional properties in "ExtraFIeldsModel" don't need index attributes, and they go in the natural order.

We really wish for the BaseModel Index to work without depending on the next Index(1) attribute. That way anyone creating new classes and inheriting BaseModel doesn't need to know about this attribute bug (I call it a bug just because Index1 is required. Index2, Index3... on other properties are not.)

Workaround:


   public class ExtraFieldsModel : BaseModel
    {
        [Index(1)] //Just 1 attribute is enough to make [Index(0)] work.
        public string FullName { get; set; }
        public string Address {get;set;}
        public DateTime DateOfBirth {get;set;}
    }

    public class BaseModel
    {
        [Index(0)]
        public string Email { get; set; }
    }

Edgaras91 avatar Sep 15 '21 07:09 Edgaras91

Why should the attributes on a base class take precedence over attributes on the child? I know that works well for you, but in general terms.

JoshClose avatar Sep 15 '21 14:09 JoshClose

It's not a matter of taking precedence I don't think - I am only using 1 attribute. It's a matter of attribute working. The attribute should take precedence over all the other property column orders, right? That's why it's there, to over-write the order? I don't think it should matter how the final model gets generated, if it inherits something or not, it's just properties included in the child class in this case?

Edgaras91 avatar Sep 15 '21 15:09 Edgaras91