Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IEnumerable<T> to a CSV file

Tags:

c#

.net

csv

linq

I am getting the result from LINQ query as var of type IEnumerable<T>

I want a CSV file to be created from the result from the LINQ

I am getting the result from the following query

var r = from table in myDataTable.AsEnumerable()
        orderby table.Field<string>(para1)
        group table by new { Name = table[para1], Y = table[para2] }
        into ResultTable
        select new
        {
            Name = ResultTable.Key,
            Count = ResultTable.Count()
        };
like image 975
usr021986 Avatar asked Jun 01 '11 10:06

usr021986


4 Answers

Check this

 public static class LinqToCSV
    {
        public static string ToCsv<T>(this IEnumerable<T> items)
            where T : class
        {
            var csvBuilder = new StringBuilder();
            var properties = typeof(T).GetProperties();
            foreach (T item in items)
            {
                string line = string.Join(",",properties.Select(p => p.GetValue(item, null).ToCsvValue()).ToArray());
                csvBuilder.AppendLine(line);
            }
            return csvBuilder.ToString();
        }

        private static string ToCsvValue<T>(this T item)
        {
            if(item == null) return "\"\"";

            if (item is string)
            {
                return string.Format("\"{0}\"", item.ToString().Replace("\"", "\\\""));
            }
            double dummy;
            if (double.TryParse(item.ToString(), out dummy))
            {
                return string.Format("{0}", item);
            }
            return string.Format("\"{0}\"", item);
        }
    }

Full code at : Scott Hanselman's ComputerZen Blog - From Linq To CSV

like image 66
Pranay Rana Avatar answered Nov 20 '22 00:11

Pranay Rana


IEnumerable<string> lines = r.Select(x => String.Format("{0},{1}", r.Name, r.Count));
System.IO.File.WriteAllLines(path, lines);

will produce:

name1,count1
name2,count2
...
like image 20
abatishchev Avatar answered Nov 19 '22 23:11

abatishchev


This is screaming out for Linq2CSV:

http://www.codeproject.com/KB/linq/LINQtoCSV.aspx

which is also available on nuget:

http://nuget.org/List/Packages/LinqToCsv

Excellent library, really recommend.

like image 2
Stuart Blackler Avatar answered Nov 20 '22 01:11

Stuart Blackler


its not clear what you realy want, but this can be a solution public void Read() {

    var r = from table in myDataTable.AsEnumerable()
            orderby table.Field<string>(para1)
            group table by new { Name = table[para1], Y = table[para2] }
                into ResultTable
                select new NameCount()
                {
                    Name = ResultTable.Key,
                    Count = ResultTable.Count()
                }.ToString();

    //Write all r to a File
}
public class NameCount
{
    public string Name { get; set; }
    public int Count { get; set; }
    public string ToString()
    {
        return string.Format("{0},{1}\r\n", Name, Count);
    }
}
like image 2
DeveloperX Avatar answered Nov 20 '22 00:11

DeveloperX