This artice will write the main step to export generic data via csv with complete code and step by step.

1.Down load EntityFramework

Install-package entityframework -v 6.2.0

2.Add EF data model with only one table for simplicity.Take the AdventureWorks2017.Sales.SalesOrderDetail for example.

3.Override the ToString() method of the  newly added data model  cs file.

public override string ToString()
{
string formatMsg = SalesOrderID + "," + SalesOrderDetailID + "," + CarrierTrackingNumber + "," +
OrderQty + "," + ProductID + "," + SpecialOfferID + "," + UnitPrice + "," + UnitPriceDiscount + "," +
LineTotal + "," + rowguid + "," + ModifiedDate + Environment.NewLine;
return formatMsg;
}

4.To be exact and precise,I will add StopWatch to log the cost in milliseconds.

The full code as below.

So far in my own pc, I can test as much as 3.8 million rows data and cost about 25 seconds as the screenshot illustrated.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Diagnostics;

namespace ConsoleApp322
{
class Program
{
static string cvsFileFullName = Directory.GetCurrentDirectory() + "\\" + DateTime.Now.ToString("yyyyMMddHHmmssffff") + ".csv";
static string fullLogFileName = Directory.GetCurrentDirectory() + "\\" + DateTime.Now.ToString("yyyyMMdd") + "log.txt";
static Stopwatch stopWatch = new Stopwatch();
static int exportNumber = 0;
static void Main(string[] args)
{
ExportTToCVS();
}

static void ExportTToCVS()
{
using (AdventureWorks2017Entities db = new AdventureWorks2017Entities())
{
List<SalesOrderDetail> orderList = db.SalesOrderDetails.ToList();
orderList.AddRange(orderList);
orderList.AddRange(orderList);
orderList.AddRange(orderList);
orderList.AddRange(orderList);
orderList.AddRange(orderList);
orderList.AddRange(orderList);
ExportListTToCVS<SalesOrderDetail>(orderList);
}
}

static void ExportListTToCVS<T>(List<T> dataList)
{

if(dataList!=null && dataList.Any())
{
stopWatch.Start();
exportNumber = dataList.Count;
StringBuilder orderBuilder = new StringBuilder();
var firstRowData = dataList.FirstOrDefault();
var orderProps = firstRowData.GetType().GetProperties().ToList();
orderBuilder.AppendLine(string.Join(",", orderProps.Select(x=>x.Name).ToArray()));
foreach(var dl in dataList)
{
orderBuilder.Append(dl.ToString());
}

using (StreamWriter streamWriter = new StreamWriter(cvsFileFullName))
{
streamWriter.WriteLine(orderBuilder.ToString());
}

stopWatch.Stop();
Process proc = Process.GetCurrentProcess();
long exportMemory = proc.PrivateMemorySize64;
string exportMsg = $"File path:{cvsFileFullName},export number:{exportNumber}," +
$"cost: {stopWatch.ElapsedMilliseconds} milliseconds,memory:{exportMemory}";
File.AppendAllText(fullLogFileName, exportMsg + Environment.NewLine);
}
}
}
}

C# CSV Generic T的更多相关文章

  1. C#, CSV,Generic, 泛型,导出

    using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threa ...

  2. .net如何向csv添加一列

    using System;using System.Collections.Generic;using System.Drawing;using System.Globalization;using ...

  3. Reading and Writing CSV Files in C#

    Introduction A common requirement is to have applications share data with other programs. Although t ...

  4. 根据日期 读取三个csv不留指定日期的内容 新保存一个文件

    using System;using System.Collections.Generic;using System.Drawing;using System.Globalization;using ...

  5. C#操作CSV存取类

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.D ...

  6. C# csv 操作类

    using System.Data; using System.IO; using System.Text; namespace YanZhiwei.DotNet2.Utilities.Common ...

  7. c# 简单文件流读写CSV文件

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.R ...

  8. 【C#】用C#通过读取数据库方式读取CSV文件

    using System; using System.Collections.Generic; using System.Linq; using System.Windows.Forms; names ...

  9. 快速读取csv平面文件,并导入数据库,简单小工具

    using DataToDeal; using LumenWorks.Framework.IO.Csv; using Microsoft.Win32; using System; using Syst ...

随机推荐

  1. Docker配置yapi接口

    一.安装Docker 第一步:yum 包更新到最新 sudo yum update 第二步:安装需要的软件包 yum-util 提供yum-config-manager功能,另外两个是devicema ...

  2. springMVC校验器(validator)

    springmvc使用的是Hibernate Validator(和Hibernate的ORM无关)来完成校验功能 一.普通校验 1.导入jar包 2.编写校验错误配置文件 3.配置校验错误信息文件 ...

  3. JAVA的基本语法1

    1.关键字 关键字的定义和特点 定义:被JAVA语言赋予了特殊含义,用作专门用途的字符串(单词). 就是在java语言编程的时候,在关键的地方使用的单词,体现关键的地方的含义.这些单词都是特有的,并且 ...

  4. vi 上下左右变ABCD乱码解决方法

    CentOS echo "set nocompatible" >> ~/.vimrc source ~/.vimrc debian sudo apt-get remov ...

  5. 用css做三角形

    <html> <body> <style> .trlangle{ width: 0; height: 0; border-left: 50px solid tran ...

  6. 【转载】更简单的学习Android事件分发

    事件分发是Android中非常重要的机制,是用户与界面交互的基础.这篇文章将通过示例打印出的Log,绘制出事件分发的流程图,让大家更容易的去理解Android的事件分发机制. 一.必要的基础知识 1. ...

  7. css伪类实现行号自动填充

    css伪类实现行号自动填充 大多数时候我们需要行号自动填充的时候我们可以 大多数时候是插入元素, 在元素里用js填入行号,或者用 ol > li 实现行号填充, 对于上面的方式,都不太灵活,而且 ...

  8. [b0006] Spark 2.0.1 伪分布式搭建练手

    环境: 已经安装好: hadoop 2.6.4  yarn 参考: [b0001] 伪分布式 hadoop 2.6.4 准备: spark-2.0.1-bin-hadoop2.6.tgz 下载地址:  ...

  9. spark Streaming与kafka的集成消费

    Spark 2.3.3    Kafka   2.11-1.0.2        Java  jdk1.8.0_191           Hbase 1.2.11 from pyspark impo ...

  10. Ubuntu 18.04安装Conda、Jupyter Notebook、Anaconda

    1.Conda是一个开源的软件包管理系统和环境管理系统,它可以作为单独的纯净工具安装在系统环境中,有的python库无法用conda获得时,conda允许在conda环境中利用Pip获取包文件.可以将 ...