Search
 
SCRIPT & CODE EXAMPLE
 

CSHARP

object list to csv c#

private void SaveToCsv<T>(List<T> reportData, string path)
{
    var lines = new List<string>();
    IEnumerable<PropertyDescriptor> props = TypeDescriptor.GetProperties(typeof(T)).OfType<PropertyDescriptor>();
    var header = string.Join(",", props.ToList().Select(x => x.Name));
    lines.Add(header);
    var valueLines = reportData.Select(row => string.Join(",", header.Split(',').Select(a => row.GetType().GetProperty(a).GetValue(row, null))));
    lines.AddRange(valueLines);
    File.WriteAllLines(path, lines.ToArray());
}
Comment

how to write a list to csv c#

File.WriteAllLines("text.txt", lst.Select(x => string.Join(",", x)));
Comment

PREVIOUS NEXT
Code Example
Csharp :: disable button in android studio 
Csharp :: an existing connection was forcibly closed by the remote host. .net core 
Csharp :: c# random 
Csharp :: how to check if List<T element contains an item with a Particular Property Value in c# 
Csharp :: get sha1 hashcode from c# 
Csharp :: c# enum 
Csharp :: c# remove item from list 
Csharp :: c# select a row from datagridview by value 
Csharp :: basic of c# sockets 
Csharp :: wpf get function name 
Csharp :: while c# 
Csharp :: solid principles c# 
Csharp :: c# wpf image source from resource programmatically 
Csharp :: c# array display 
Csharp :: C# select keyword lambda 
Csharp :: how to compare datetime in c# 
Csharp :: csharp get decimal part of number 
Csharp :: how to get the size an array in unity 
Csharp :: how to get rid of the slashes in datetime variables c# 
Csharp :: c# function 
Csharp :: c# scroll to bottom of datagridview vb.net 
Csharp :: random string generator c# 
Csharp :: C# extract all of a property from a list of objcets 
Csharp :: c# chunk array linq 
Csharp :: c# wpf row definition height * in code 
Csharp :: how to keep rigidbody2D upright unity 
Csharp :: convert c# string to int 
Csharp :: frustum 
Csharp :: C# get column of 2d array 
Csharp :: preprocessors 
ADD CONTENT
Topic
Content
Source link
Name
3+7 =