C# Export DataModel to a CSV file

I want to export a datamodel to a CSV file.  I have the below sample code.  How do you get the value of each column in the datamodel that is passed?  How do you get the column names of the datamodel that is passed?  Value does hold the datamodel.
public class ExportExcelCVS<T> : IExportData<T>
{
    public void ExportData(List<T> value)
    {
        var model = value;
        using (StreamWriter sw = new StreamWriter(AppSettings.GetExportLocation(), false))
        {
                    //BELOW IS NOT WORKING
                    Type type = model.GetType();
                    PropertyInfo[] properties = type.GetProperties();

                    foreach (PropertyInfo property in properties)
                    {
                        Console.WriteLine("Name: " + property.Name + ", Value: " + property.GetValue(model, null));
                    }
        }
    }
}

Open in new window

LVL 2
CipherISAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Nitin SontakkeDeveloperCommented:
First, I guess, just remove the line number 5 from above code. Doesn't do anything, I believe.

On line 9 above, you will need to take the type of T so it should be

Type type = typeof(T);

Open in new window


Do these changes and try. Rest all looks okay to my bare eyes.
0
Nitin SontakkeDeveloperCommented:
Line 14 also doesn't look right, actually. Remember you have a List<T>. And you are iterating the properties of the List<T> and not that of T. Technically, you should be iterating over each instance of T in List<T> and then iterating through each property of T.

Does that make sense?
0
CipherISAuthor Commented:
So, Type type = typeof(T); is allowing me to get the header values.  I'm trying to figure out how to loop through List<T>.
 public void ExportData(List<T> value)
{
    try
    {
        var model = value;
        using (StreamWriter sw = new StreamWriter(AppSettings.GetExportLocation(), false))
        {
             Type type = typeof(T);
             PropertyInfo[] properties = type.GetProperties();

             foreach (PropertyInfo property in properties)
             {
                        Console.WriteLine("Name: " + property.Name + ", Value: " + property.GetValue(property, null));
             }
        }
    }
    catch (Exception ex)
    {
            string err = ex.Message;
     }
}

Open in new window

0
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

Nitin SontakkeDeveloperCommented:
I am obviously not 100% sure of what I am typing but you can get an idea and give it a go.

foreach(T instance in value)
{
  foreach (PropertyInfo property in properties)
  {
    Console.WriteLine("Name: " + property.Name + ", Value: " + property.GetValue(instance, null));
  }
}

Open in new window


Furthermore, remember (I think) it works only for intrinsic data types. Which means, doesn't work if property is of type Object.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Nitin SontakkeDeveloperCommented:
Remember on first line (header) you will only write property.Name and within loop (data)  you will write property.GetValue(instance, null).
1
CipherISAuthor Commented:
That was it.  Thanks so much.
0
Nitin SontakkeDeveloperCommented:
For completeness, I suggest, you enclose all your string values within double quotes. This protects data being skewed if it contains comma, the delimiter in this case.
1
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.