2015-05-22 15 views
0

我正在尋找一些關於如何將許多不同清單合併到捕獲每個清單的每個元素的清單中的一些提示/建議。如何將不同格式的清單合併到一個清單中?

所以說我有以下3種具有以下格式的清單。正如你可以看到一些元素重複,一些是清單獨特的。該清單還包含每個元素下的數千行。

清單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| 

正如您所看到的,每個字段都是從原始清單中捕獲並保存到一個主清單中。如果某個元素在特定的清單中不可用,則該行保留爲空。

+1

您是指應用程序清單文件,它是XML格式?你在這裏顯示一些隨機表格數據..... – Claies

+1

請澄清你的問題,我們不知道你指的是「manifest」,因爲它看起來與Application Manifest文件完全無關。 – Dai

+0

您的術語存在很多問題,因此難以或不可能理解這是什麼。 「Manifest」沒有解釋,「元素」實際上應該被稱爲「列」,而當你使用「row」這個詞時,我認爲你實際上是指「cell」或「element」。 – RenniePet

回答

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)); 

    } 
}