亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

將數據從嵌套的 c# 類導出到 csv 文件

將數據從嵌套的 c# 類導出到 csv 文件

C#
青春有我 2021-11-14 15:08:00
我有以下類可以使用,我想將內容導出/展平到 csv 文件。我曾嘗試使用 SelectMany,但找不到從公司級別和帶有 SalesArea、Node 和 EthernetArea 的底層級別為我的 csv 文件選擇字段的方法。有人可以解釋我需要做什么來實現這一目標嗎?public class Rootobject{    public Company[] Companies { get; set; }}public class Company{    public string ORG_NAME { get; set; }    public SalesArea[] Salesareas { get; set; }    public Node[] Nodes { get; set; }    public EthernetArea[] Ethernetareas { get; set; }}public class SalesArea{    public string OBJ_NAME { get; set; }    public string AREA_CTYPE { get; set; }}public class Node{    public string OBJ_NAME { get; set; }    public object BUILDING { get; set; }}public class EthernetArea{    public string OBJ_NAME { get; set; }    public string AREA_CTYPE { get; set; }    public Product[] Products { get; set; }}public class Product{    public string BANDWIDTH_A { get; set; }    public string CONNECTION_TYPE { get; set; }}
查看完整描述

2 回答

?
慕尼黑的夜晚無繁華

TA貢獻1864條經驗 獲得超6個贊

我確定應該有另一種方法來做到這一點,但無論如何,這就是我所做的......


如果我理解正確,您想要“非規范化”數據并將集合導出到 CSV。


我用了很多Select SelectMany和Join


var root = new Rootobject

    {

        Companies = new Company[] {

            new Company

        {

            ORG_NAME = "Co",

            Salesareas = new SalesArea[]{

            new SalesArea {

                OBJ_NAME = "Sa",

                AREA_CTYPE = "SaAr",

            }

        },

            Nodes = new Node[] {

            new Node {

                OBJ_NAME = "No",

                BUILDING = "NoBu"

            }

        },

            Ethernetareas = new EthernetArea[] {

            new EthernetArea {

                OBJ_NAME = "Et",

                AREA_CTYPE = "EtAr",

                Products = new Product[] {

                    new Product {

                        BANDWIDTH_A = "ProA",

                        CONNECTION_TYPE = "ProCon"

                    }

                }

            }

        }

        },

        new Company

        {

            ORG_NAME = "Co2",

            Salesareas = new SalesArea[]{

            new SalesArea {

                OBJ_NAME = "Sa2",

                AREA_CTYPE = "SaAr2",

            }

        },

            Nodes = new Node[] {

            new Node {

                OBJ_NAME = "No2",

                BUILDING = "NoBu2"

            }

        },

            Ethernetareas = new EthernetArea[] {

            new EthernetArea {

                OBJ_NAME = "Et2",

                AREA_CTYPE = "EtAr2",

                Products = new Product[] {

                    new Product {

                        BANDWIDTH_A = "ProA2",

                        CONNECTION_TYPE = "ProCon2"

                    },

                    new Product {

                        BANDWIDTH_A = "ProA3",

                        CONNECTION_TYPE = "ProCon3"

                    }

                }

            }

        }

        }

    }

    };


    var sas = root.Companies.SelectMany(x => x.Salesareas.Select(y => new { Company = x.ORG_NAME, SalesName = y.OBJ_NAME, SalesAreaType = y.AREA_CTYPE }));

    var nodes = root.Companies.SelectMany(x => x.Nodes.Select(y => new { Company = x.ORG_NAME, NodesName = y.OBJ_NAME, NodeBuilding = y.BUILDING }));

    var ethes = root.Companies.SelectMany(x => x.Ethernetareas.SelectMany(y => y.Products .Select(z => new { Company = x.ORG_NAME, EthernetName = y.OBJ_NAME, EthernetArea = y.AREA_CTYPE, BandwithA = z.BANDWIDTH_A, ConnnectionType = z.CONNECTION_TYPE })));


    sas.Join(nodes, x => x.Company, y => y.Company, (x, y) => new {x.Company, x.SalesName, x.SalesAreaType, y.NodesName, y.NodeBuilding})

        .Join(ethes, x => x.Company, y => y.Company, (x, y) => new {x.Company, x.SalesName, x.SalesAreaType, x.NodesName, x.NodeBuilding, y.EthernetName, y.EthernetArea, y.BandwithA, y.ConnnectionType})

        .Dump();

Dump()是一個LinqPad擴展方法

這是結果......

http://img1.sycdn.imooc.com//6190b6100001ac5b12260289.jpg

查看完整回答
反對 回復 2021-11-14
?
慕標5832272

TA貢獻1966條經驗 獲得超4個贊

定義一個類,該類將在 CSV 中表示您想要的內容。由于多個對象屬性在您的模型中具有相同的名稱。

在您的評論中,您選擇使用類名作為屬性的前綴,因此您應該具有以下內容:


public class CompanyCSV

{

    public string Company_ORG_NAME { get; set; }


    public string SalesArea_OBJ_NAME { get; set; }

    public string SalesArea_AREA_CTYPE { get; set; }


    public string Node_OBJ_NAME { get; set; }

    public string Node_BUILDING { get; set; }


    public string EthernetArea_OBJ_NAME { get; set; }

    public string EthernetArea_AREA_CTYPE { get; set; }


    public string Product_BANDWIDTH_A { get; set; }

    public string Product_CONNECTION_TYPE { get; set; }

}

然后簡單地:


var result = from c in dataInput.Companies

                from s in c.Salesareas

                from n in c.Nodes

                from e in c.Ethernetareas

                from p in e.Products

                select new CompanyCSV

                {

                    Company_ORG_NAME = c.ORG_NAME,


                    SalesArea_OBJ_NAME = s.OBJ_NAME,

                    SalesArea_AREA_CTYPE = s.AREA_CTYPE,


                    Node_OBJ_NAME = n.OBJ_NAME,

                    Node_BUILDING = n.BUILDING.ToString(),


                    EthernetArea_OBJ_NAME = e.OBJ_NAME,

                    EthernetArea_AREA_CTYPE = e.AREA_CTYPE,


                    Product_BANDWIDTH_A = p.BANDWIDTH_A,

                    Product_CONNECTION_TYPE = p.CONNECTION_TYPE

                };



foreach (var line in result)

{ // Don't use this to generate your csv this is simply to show the result

    Console.WriteLine($"{line.c},{line.s},{line.n},{line.e},{line.p}, ..etc ..");

}

要編寫您的 CSV,您可以使用CsvHelper。這將幫助您使用逗號或引號處理值。不要手動生成 CSV。


如果您的集合(節點 []、公司 [] 等)可以為空,您應該在 getter 中使用空合并運算符來保護這種邊緣情況,例如:


private Node[] nodes;

public Node[] Nodes {

    get { return nodes ?? (nodes = new Node[] { }); }

    set { nodes = value; }

}


查看完整回答
反對 回復 2021-11-14
  • 2 回答
  • 0 關注
  • 308 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號