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擴展方法
這是結果......

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; }
}
- 2 回答
- 0 關注
- 308 瀏覽
添加回答
舉報