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 :: detect trigger in unity 
Csharp :: detect keypress c# 
Csharp :: Generate UUID in c# 
Csharp :: unity set active for seconds 
Csharp :: vb.net open file with default program 
Csharp :: add admin priviledge to c# program 
Csharp :: solve fizzbuz c# 
Csharp :: blazor alert 
Csharp :: c# read from text documenmt 
Csharp :: unity object to mouse position 
Csharp :: how refresh just one table in laravel by terminal 
Csharp :: message uwp c# 
Csharp :: convert string to array c# 
Csharp :: how to get the path of the current directory in c# 
Csharp :: text split 
Csharp :: rigidbody.addtorque 
Csharp :: drag png to unity 3d 
Csharp :: how to freeze x and y position in rb2d with code unity 
Csharp :: unity change tmp text from script 
Csharp :: C# .NET Core linq Distinct 
Csharp :: unity how to get the first word from string 
Csharp :: Compare trees 
Csharp :: singleton unity 
Csharp :: c# string default value 
Csharp :: .net core check if user is logged in 
Csharp :: VLC .net 
Csharp :: know to which direction Vector.MoveTowards is moving unity 2D 
Csharp :: change sprite of a sprite unity 
Csharp :: key value pair in c# 
Csharp :: c# do while loop 
ADD CONTENT
Topic
Content
Source link
Name
4+8 =