Created
October 20, 2015 12:24
-
-
Save anonymous/0c2fc8bc1a1ffbdf2b2f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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