Click here to Skip to main content
15,883,535 members
Articles / General Programming / File

Quick, Simple and Elegant way to convert C# data model to CSV

Rate me:
Please Sign up or sign in to vote.
5.00/5 (2 votes)
18 Aug 2019CPOL1 min read 9.8K   114   6   6
CSV file generator using simplistic approach


Converts a C# data model to CSV formatted file. I know there are tools which do this, but I had a situation where I can't use third party libraries or tools and I had to write code for a legacy application.

Could be a good starting point or use this as template.

All it does is pass on the data to the model and it converts it to CSV data or file.


There are multiple ways to achieve this. One of them is Reflection where we use reflection to get the types of the properties and then get the data, loop through all properties and generate the data.

In this article, I would specify a simple and elegant way to achieve the same. The approach really depends on the situation and problem you have.

Using the Code

Let's start with some test data as input:

    "MemberName": "David",
    "MemberNumber": "1"
    "MemberName": "Joseph",
    "MemberNumber": "2"

Deserialize the data:

var listMemberData=JsonConvert.DeserializeObject<IEnumerable<MemberData>>

Define ColumnDataMapper - Maps each of the cells:

public class ColumnDataMapper
        public string HeaderName { get; set; }
        public Func<MemberData, string> Extractor { get; set; }

Now define the initial structure of our Member Data (like a table having row and column):

public static readonly IEnumerable<ColumnDataMapper> MemberDataMappers = new[]
            new ColumnDataMapper
                HeaderName = "Member Number",
                Extractor = obj => obj.MemberNumber.ToString()
            new ColumnDataMapper
                HeaderName = "Member Name",
                Extractor = obj => obj.MemberName

Generate all data:

public static StringBuilder GenerateFileContent(IEnumerable<MemberData> listMemberData)
            StringBuilder sbOutput = new StringBuilder();

            //Generate Header
             (DataSeparator, DataMappers.MemberDataMappers.Select(p => p.HeaderName)));

            //Generate rows
            foreach (var memberData in listMemberData)
            return sbOutput;

Points of Interest

For simple structure, we could go with this method but for complex objects, I'd prefer Reflection or open source tool like FileHelpers Library.


  • 19th August, 2019: Initial article


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Written By
Software Developer (Senior)
Australia Australia
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

QuestionCSV standard Pin
wvd_vegt20-Aug-19 13:12
professionalwvd_vegt20-Aug-19 13:12 
AnswerRe: CSV standard Pin
rohitsies25-Aug-19 15:45
rohitsies25-Aug-19 15:45 
QuestionAn Alternative Approach Pin
George Swan19-Aug-19 10:22
mveGeorge Swan19-Aug-19 10:22 
AnswerRe: An Alternative Approach Pin
rohitsies19-Aug-19 13:23
rohitsies19-Aug-19 13:23 
GeneralRe: An Alternative Approach Pin
George Swan19-Aug-19 18:54
mveGeorge Swan19-Aug-19 18:54 
GeneralRe: An Alternative Approach Pin
rohitsies19-Aug-19 22:38
rohitsies19-Aug-19 22:38 
Awesome. Thanks for sharing.

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.