更新:使用我的类对象中的临时成分,迭代器用于解决下面lambda表达式中的zindex问题。
下面是片段:
var yValues = new List<Schema.W[]>();
Schema.W[] y = null;
for (int i = 0; i < headers.Length; i++)
{
y= data
.Select((z) => new Schema.W
{
property_0 = z[0],
property_1 = decimal.Parse(z[i + 1])
}).ToArray();
yValues.Add(y);
}
}然后最后:
var finalList = new List<Schema.W>();
Schema.W = null;
for (int i = 0; i < headers.Length; i++)
{
mapping = new Schema.W
{
innerNodes = yValues[i]
};
finalList.Add(mapping);
}更新:找到LINQ解决方案。代码审核已在场外完成。
我使用.NET中的序列化工具将*.XML文件转换为类对象,该类对象表示第三方黑匣子应用程序的数据模型。
使用LINQ,我能够将数据矩阵的列标题映射到它们位于该类对象中的组成属性中,并将其序列化到一个临时的*.XML测试文件中,获得良好的结果。解决办法如下:
Schema.W[] mapping = headers
.Select((x, index) => new Schema.W
{
// String headers (except for the first column) - "Dates" are excluded from headers
property_0 = headers[index],
property_1 = y
}).ToArray();
Schema.Entry[] y = data
.Select((z, index) => new W.Entry
{
property_0 = z[0],
property_1= decimal.Parse(z[index]) // breaks when z[index]
}).ToArray();但是,当我试图将相同的成语应用于y时,索引器会出现问题。对于要映射到的内部XML节点的第一个属性("Date"),属性映射工作正常,因为这是数据矩阵的第一列,由硬编码值(例如z)表示。
但是,我的第二个属性(" Value ")遇到了(a)值= decimal.Parse(zindex)和(b)类型问题,当我试图从处理步骤中提取单个值时,在处理步骤中将数字信息存储在IEnumerable中。当我试图将每个列的编号写入property_1字段时,会出现这个问题,该字段稍后将输出到具有以下结构的序列化XML文件中:
<W>
<Overrides>
<Entry>
<property_0>2001-01</property_0>
<property_1>90.01</property_1>
</Entry>
<Entry>
<property_0>2002-01</property_0>
<property_1>90.02</property_1>
</Entry>
<Entry>
<property_0>2003-01</property_0>
<property_1>90.03</property_1>
</Entry>
</Overrides>
</W>
<W>
<Overrides>
<Entry>
<property_0>2001-01</property_0>
<property_1>90.01</property_1>
</Entry>
<Entry>
<property_0>2002-01</property_0>
<property_1>90.02</property_1>
</Entry>
<Entry>
<property_0>2003-01</property_0>
<property_1>90.03</property_1>
</Entry>
</Overrides>
</W>目前,第二列的内容正在写入第二个节点,而我的CSV文件中的第三列的内容应该被写入(property_1 = {9001,9002,9003})。
我尝试了以下方法: a.将LINQ序列应用到没有日期列(例如property_0)的CSV数据矩阵中,并且只将我的选择应用于数值(例如[90.01,90.02,90.03,9001,9002,9003]。当映射到我的类对象时,使用FileHelpers来“忽略”第一列。
我最后想要的是:
<W>
<Overrides>
<Entry>
<property_0>2001-01</property_0>
<property_1>90.01</property_1>
</Entry>
<Entry>
<property_0>2002-01</property_0>
<property_1>90.02</property_1>
</Entry>
<Entry>
<property_0>2003-01</property_0>
<property_1>90.03</property_1>
</Entry>
</Overrides>
</W>
<W>
<Overrides>
<Entry>
<property_0>2001-01</property_0>
<property_1>9001</property_1>
</Entry>
<Entry>
<property_0>2002-01</property_0>
<property_1>9002</property_1>
</Entry>
<Entry>
<property_0>2003-01</property_0>
<property_1>9003</property_1>
</Entry>
</Overrides>
</W>任何与重构此解决方案集相关的指导也将受到高度赞赏。这对我来说是最后的手段--也许我会因为这是我的第一篇文章而受到一些批评,因为我的个人资料没有任何的贡献,这是正确的。不幸的是,文档在语义方面没有帮助。
谢谢各位。如果没有堆栈溢出,我们中的许多人就会不知所措。
发布于 2019-04-15 01:18:17
更新:这里的是一个重构的方法,用于下面的代码片段,以及最终如何更好地解决原始问题.新代码从这里开始:
private static Schema.W[] Map(string[] headers, IEnumerable<string[]> data)
{
Schema.W[] mapping = headers
.Select((x, index) => new Schema.W
{
property_0 = x,
property_1 = data
.Select((z) => new Schema.WEntry
{
child_0 = z[0],
child_1 = z[index + 1]
}).ToArray()
}).ToArray();
return mapping;
}旧代码从这里开始:
var yValues = new List<Schema.W[]>();
Schema.W[] y = null;
for (int i = 0; i < headers.Length; i++)
{
y = data
.Select((z) => new Schema.W
{
property_0 = z[0],
property_1 = decimal.Parse(z[i + 1])
}).ToArray();
yValues.Add(y);
}
}然后最后:
var finalList = new List<Schema.W>();
Schema.W = null;
for (int i = 0; i < headers.Length; i++)
{
mapping = new Schema.W
{
innerNodes = yValues[i]
};
finalList.Add(mapping);
}https://stackoverflow.com/questions/55680177
复制相似问题