我正在寻找关于如何将一些不同的清单合并到一个清单中的一些提示/建议,该清单捕获每个清单的每个元素。
所以说我有以下三种清单,格式如下。您可以看到一些元素重复,一些元素是清单独有的。此类清单还包含每个元素下的数千行。
清单1:
Element1|Element2|Element4|Element5|
00000001|00000002|00000004|00000005|
清单2:
Element2|Element3|Element4|Element5|
00000002|00000003|00000004|00000005|
清单3:
Element1|Element3|Element4|Element6|
00000001|00000003|00000004|00000006|
我想要的最终清单看起来像这样:
Element1|Element2|Element4|Element5|Element3|Element6|
00000001|00000002|00000004|00000005|00000003|________|
________|00000002|00000004|00000005|________|________|
00000001|________|00000004|________|00000003|00000006|
正如您所看到的,每个字段都是从原始清单中捕获并保存到一个主清单中。如果某个元素在特定清单中不可用,则该行将为空。
答案 0 :(得分:0)
所以,我不确定你的意思是什么" Manifest",对输入/输出文件结构或它来自哪里一无所知等等。我假设是CSV数据,所以这里是一个例子。希望它能帮助你实现目标。
class Program
{
static void Main(string[] args)
{
var merger = new CsvMerger(Console.Out);
Guid type1 = merger.RegisterType(new [] { "Element1", "Element2", "Element3" });
Guid type2 = merger.RegisterType(new[] { "Element3", "Element5", "Element6" });
merger.WriteHeaders();
merger.WriteData(type1, "00000001", "00000002", "00000003");
merger.WriteData(type2, "00000003", "00000005", "00000006");
}
}
public class CsvMerger
{
readonly TextWriter output;
const string OutputSeparator = "|";
bool canAddTypes = true;
readonly List<string> fieldNames = new List<string>();
readonly Dictionary<Guid, int[]> positionMap = new Dictionary<Guid, int[]>();
public CsvMerger(TextWriter output)
{
this.output = output;
}
public Guid RegisterType(params string[] dataLayout)
{
if (!this.canAddTypes)
throw new InvalidOperationException("Already started writing data, cannot add more source types");
int[] positions = new int[dataLayout.Length];
for (int i = 0; i < dataLayout.Length; i++)
{
int position = this.fieldNames.IndexOf(dataLayout[i]);
if (position == -1)
{
positions[i] = this.fieldNames.Count;
this.fieldNames.Add(dataLayout[i]);
}
else
{
positions[i] = position;
}
}
Guid typeKey = Guid.NewGuid();
this.positionMap[typeKey] = positions;
return typeKey;
}
public void WriteHeaders()
{
Console.WriteLine(string.Join(OutputSeparator, this.fieldNames));
}
public void WriteData(Guid type, params string[] data)
{
this.canAddTypes = false;
int[] map = this.positionMap[type];
string[] arrangedDataBuffer = new string[this.fieldNames.Count];
for (int i = 0; i < data.Length; i++)
{
arrangedDataBuffer[map[i]] = data[i];
}
this.output.WriteLine(string.Join(OutputSeparator, arrangedDataBuffer));
}
}