我有以下类要使用,我想将内容导出/展平为csv文件。 我尝试使用SelectMany,但找不到从Salesforce,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; }
}
答案 0 :(得分:1)
我确定应该有另一种方法,但是无论如何,这就是我的方法...
如果我正确理解,您想对数据进行“非规范化”并将集合导出为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扩展方法这是结果...
对于集合,您可以按照CsvHelper的建议使用@Drag And Drop来生成CSV文件。...:)
我刚刚意识到@Drag And Drop代码更直接,更干净,更好。...:)
答案 1 :(得分:0)
定义一个类,该类将表示您想要在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。
如果您的集合(Node [],Company []等)可以为null,则应在吸气剂中使用null-coalescing operator来保护这种边缘情况,例如:
private Node[] nodes;
public Node[] Nodes {
get { return nodes ?? (nodes = new Node[] { }); }
set { nodes = value; }
}