Search
 
SCRIPT & CODE EXAMPLE
 

CSHARP

how do I write to a csv file from c# using entity framework

public class CsvFileWriter
{
    public static void WriteToFile<T>(string filePath, List<T> objs, string[] propertyNames)
    {
        var builder = new StringBuilder();
        var propertyInfos = RelevantPropertyInfos<T>(propertyNames);
        foreach (var obj in objs)
            builder.AppendLine(CsvDataFor(obj, propertyInfos));

        File.WriteAllText(filePath, builder.ToString());
    }

    public static void WriteToFileSingleFieldOneLine<T>(string filePath, List<T> objs, string propertyName)
    {
        var builder = new StringBuilder();
        var propertyInfos = RelevantPropertyInfos<T>(new[] { propertyName });
        for (var i = 0; i < objs.Count; i++)
        {
            builder.Append(CsvDataFor(objs[i], propertyInfos));

            if (i < objs.Count - 1)
                builder.Append(",");
        }

        File.WriteAllText(filePath, builder.ToString());
    }

    private static List<PropertyInfo> RelevantPropertyInfos<T>(IEnumerable<string> propertyNames)
    {
        var propertyInfos = typeof(T).GetProperties().Where(p => propertyNames.Contains(p.Name)).ToDictionary(pi => pi.Name, pi => pi);
        return (from propertyName in propertyNames where propertyInfos.ContainsKey(propertyName) select propertyInfos[propertyName]).ToList();
    }

    private static string CsvDataFor(object obj, IList<PropertyInfo> propertyInfos)
    {
        if (obj == null)
            return "";

        var builder = new StringBuilder();

        for (var i = 0; i < propertyInfos.Count; i++)
        {
            builder.Append(propertyInfos[i].GetValue(obj, null));

            if (i < propertyInfos.Count - 1)
                builder.Append(",");
        }

        return builder.ToString();
    }
}
Comment

PREVIOUS NEXT
Code Example
Csharp :: how to configure asp.net core on ionon 1&1 hosting 
Csharp :: get path c# application 
Csharp :: c# print expression tree 
Csharp :: c# object to xmldocument 
Csharp :: how to find all role in mysql 
Csharp :: last word of string to uppercase c# 
Csharp :: 1/1/1/1/1 
Csharp :: csv to dataset c# 
Csharp :: linq cheat sheet 
Csharp :: c# task call more web api in parallel 
Csharp :: wpf clock conrt 
Csharp :: unity check if animator has parameter 
Csharp :: mental retardation 
Csharp :: wpf repository pattern query async with includes properties 
Csharp :: telerik mvc grid editable date no time 
Csharp :: how to add an embedded resource in visual studio code 
Csharp :: get path revit link unloaded 
Csharp :: loops in coding 
Csharp :: download xml file asp.net web api 
Csharp :: nest elasticsearch date reange c# .net 
Csharp :: accord.io read .mat file 
Csharp :: unity move in x seconds to pos 
Csharp :: telerik mvc grid round sum result 
Csharp :: Get dwgexport setting reivit api 
Csharp :: check for held hey unity 
Csharp :: c# how to convert string to float 
Csharp :: Garbage collect every 30 frames unity 
Csharp :: uity pause game 
Csharp :: asp.net web hooks 
Csharp :: c# unhandled exception in thread” 
ADD CONTENT
Topic
Content
Source link
Name
1+6 =