• C# CSV Generic T


    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);
    }
    }
    }
    }

  • 相关阅读:
    mysql关联取附表最后一条记录,附加lareval orm实现
    lumen 常用辅助函数
    Lumen Carbon 日期及时间处理包
    $_SERVER,IP,域名常用方法
    上传Docker镜像到阿里云
    connect() failed (111: Connection refused) while connecting to upstream, cli
    linux使用常见问题
    docker实用命名
    yii 常用orm
    Hibernate多对多映射(双向关联)实例详解——真
  • 原文地址:https://www.cnblogs.com/Fred1987/p/10344396.html
Copyright © 2020-2023  润新知