Search code examples
c#csvservicestackservicestack-text

csv change order of the columns


I am currently using ServiceStack.Text to serialize CSV from a list of objects. I have a model:

public class UploadDocument
{

[DataMember(Name = "Patient")]
public string Patient { get; set; }

[DataMember(Name = "Patient First Name")]
public string PatientFirstName { get; set; }

[DataMember(Name = "Patient Last Name")]
public string PatientLastName { get; set; }

[DataMember(Name = "Email")]
public string Email { get; set; }

}

The result is:

Patient,    Patient First Name, Patient Last Name,  Email
XXX,        YYY,                ZZZZZ,              [email protected],    
XXX,        YYY,                ZZZZZ,              [email protected],    
XXX,        YYY,                ZZZZZ,              [email protected],   

However, for another report I need to change the order of the csv and return:

Email,               Patient,    Patient First Name, Patient Last Name,  
[email protected],   XXX,        YYY,                ZZZZZ,                  
[email protected], XXX,        YYY,                ZZZZZ,                  
[email protected],    XXX,        YYY,                ZZZZZ, 

Is there a way to do that avoiding create a new model?


Solution

  • You can use the order option. Ex: [DataMember(Order = 0)]

    the documentation is at this link: https://learn.microsoft.com/en-us/dotnet/framework/wcf/feature-details/data-member-order