Search
 
SCRIPT & CODE EXAMPLE
 

CSHARP

C# ToCsv Extension Method

public static string ToCsv<T>(this IEnumerable<T> instance, bool includeColumnHeader, string[] properties)
		{
			if (instance == null)
				return null;

			var csv = new StringBuilder();

			if (includeColumnHeader)
			{
				var header = new StringBuilder();
				foreach (var property in properties)
					header.AppendFormat("{0},", property);

				csv.AppendLine(header.ToString(0, header.Length - 1));
			}

			foreach (var item in instance)
			{
				var row = new StringBuilder();

				foreach (var property in properties)
					row.AppendFormat("{0},", item.GetPropertyValue<object>(property));

				csv.AppendLine(row.ToString(0, row.Length - 1));
			}

			return csv.ToString();
		}

		public static string ToCsv<T>(this IEnumerable<T> instance, bool includeColumnHeader)
		{
			if (instance == null)
				return null;

			var properties = (from p in typeof(T).GetProperties()
							  select p.Name).ToArray();

			return ToCsv(instance, includeColumnHeader, properties);
		}
Comment

PREVIOUS NEXT
Code Example
Csharp :: jobject alternative in system.text.json 
Csharp :: csharp attributes as generics constraints 
Csharp :: VBNet dictionary for each 
Csharp :: IOException: Failed to prepare target build directory. Is a built game instance running? UnityEditor.WindowsStandalone.WindowsDesktopStandalonePostProcessor.DeleteDestination (UnityEditor.Modules.BuildPostProcessArgs args) 
Csharp :: set time on audio source unity 
Csharp :: Delayed respawn timer 
Csharp :: dadar pincode 
Csharp :: disable button netbeans 
Csharp :: ef save changes 
Csharp :: How to cache database tables to prevent many database queries in Asp.net C# mvc 
Csharp :: how to make font factory text to bold in c# 
Csharp :: c# fold sum array 
Csharp :: c# iterate xml 
Csharp :: camera is rendering black screen unity 
Csharp :: enzymes chemical factory 
Csharp :: how to input message ox in c# 
Csharp :: what is implicit keyword c# 
Csharp :: edit database from datagridview with right click on data c# 
Csharp :: how to perform drop down when click on combobox in c# net 
Csharp :: c# does readonly improve performance 
Csharp :: C# assign integer 
Csharp :: tostring vb.net format decimal value with comma 
Csharp :: How to execute a script after the c# function executed 
Csharp :: binary search between two indexes 
Csharp :: Custom Circular Picture Box C# win Form app 
Csharp :: when should i use struct rather than class in c# 
Csharp :: c# e-mail send 
Csharp :: the range data annotation attribute (Double) 
Csharp :: single or default in c# 
Csharp :: Request ID: XPBBR4XG1UWuX6fWF08_-jzYkrommVJjO7Os50CTYuZmiw7kMsFUkw== 
ADD CONTENT
Topic
Content
Source link
Name
8+2 =