C#: Converting from Generic List to CSV and CSV to Generic List

CSV from List

public string GetCsvFromList(List<string> list)
{
    return String.Join(",", list.Select(x => x.ToString()).ToArray());
}

List from CSV

public List<T> GetListFromCsv<T>(string commaSplited)
{
    return (from e in commaSplited.Split(',')
            select (T)Convert.ChangeType(e.Trim(), typeof(T))).ToList();
}
Advertisements

One thought on “C#: Converting from Generic List to CSV and CSV to Generic List

  1. Greetings! Very useful advice within this article! It is the little changes that make the most important changes. Many thanks for sharing!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s