Skip to content

Instantly share code, notes, and snippets.

Created October 20, 2015 12:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/0c2fc8bc1a1ffbdf2b2f to your computer and use it in GitHub Desktop.
Save anonymous/0c2fc8bc1a1ffbdf2b2f to your computer and use it in GitHub Desktop.
public static class CollectionExtention
{
public static string ToCsv<T>(this IEnumerable<T> source, string delimiter = ",")
{
return
source
//Go Throu all elements
.Select(s => typeof(T)
.GetProperties()
//go throu all Propertys
.Select(e =>
//null check then use Placeholder
(e.GetValue(s) ?? DBNull.Value)
.ToString()
)
//combine to single Line
.Aggregate((e, f) => e + delimiter + f))
//combine to document
.Aggregate((e, f) => e + Environment.NewLine + f);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment