2011-03-11 20 views
0

如果第一個字段相同,我很難在逗號分隔的文本文件中組合一些行。這是我的文件:組合文本文件行c#

Area 1 Chiller Grids aB aN and a1 a6,test,1,Ground Floor Framing 
Area 1 Chiller Grids aB aN and a1 a6,test,2,Monorails West 
Area 1 Chiller Grids aB aN and a1 a6,test,6,Roof Framing West 
Area 1 Chiller Grids aB aN and a1 a6,test,11,Bullnose West 
Area 2 Office Grids aN zA and a1 a6,101806,3,First Floor Framing East 
Area 2 Office Grids aN zA and a1 a6,101806,5,Mezz Floor Framing 
Area 2 Office Grids aN zA and a1 a6,101806,7,Roof Framing East 
Area 2 Office Grids aN zA and a1 a6,101806,9,First Floor Ceiling 
Area 3 Bridge Grids yA yE and a1 10x,101807,4,Link Floor Framing 
Area 3 Bridge Grids yA yE and a1 10x,101807,8,Link Roof Framing 
Area 3 Bridge Grids yA yE and a1 10x,101807,10,Bridge Catwalks 
Area 3 Bridge Grids yA yE and a1 10x,101807,13,Stair 1 

我需要的輸出是這樣的:

Area 1 Chiller Grids aB aN and a1 a6,test,1-2-6-11,Ground Floor Framing-Monorails West-Roof Framing West-Bullnose West 
Area 2 Office Grids aN zA and a1 a6,101806,3-5-7-9,First Floor Framing East-Mezz Floor Framing-Roof Framing East-First Floor Ceiling 
Area 3 Bridge Grids yA yE and a1 10x,101807,4-8-10-13,Link Floor Framing-Link Roof Framing-Bridge Catwalks-Stair 1 

我怎麼能去呢? 我已經嘗試了一些LINQ的代碼,我上找到這裏,但它並沒有完全得到我想要什麼,我不知道如何使它做到這一點:

   var a = System.IO.File.ReadAllLines(@"X:\Steelcad_Project_Files\" + dir + "\\manager1.txt"); 
       var b = System.IO.File.ReadAllLines(@"X:\Steelcad_Project_Files\" + dir + "\\manager.txt"); 

       var query = 
        from bline in b 
        let parts = bline.Split(',') 
        group parts[2] by parts[0] into bg 
        join aline in a on bg.Key equals aline 
        select aline + "," + string.Join("-", bg.ToArray()); 

       System.IO.File.WriteAllLines(@"X:\Steelcad_Project_Files\" + dir + "\\result.txt", query.ToArray()); 

凡manager1文件只有第一如下所示:

Area 1 Chiller Grids aB aN and a1 a6 
Area 2 Office Grids aN zA and a1 a6 
Area 3 Bridge Grids yA yE and a1 10x 

我想,如果可能,只能使用1個文件。 任何幫助將不勝感激。謝謝

編輯:是否有可能基於行0,然後行2排序行?如果是1-11-15-2-6,它可以重新安排到1-2-6-11-15,但是保留所有的區域1在一起等等?

+0

這讓我想知道,你爲什麼從2個單獨的文件中讀取您的問題時,只提到1個輸入? – 2011-03-11 03:41:20

+0

,因爲我發現的例子是從2開始閱讀..... – Mutley 2011-03-11 03:54:29

+0

然後這個例子不適合你。你顯然是從1個文件讀取併合並行。我懷疑這個例子所做的是將1個文件中的記錄合併到另一個文件中,這不是你想要的。其他人提供的一些答案應該適合你。 – 2011-03-11 03:57:29

回答

3
var output = File.ReadAllLines(@"D:\\manager.txt") 
    .Select(line => line.Split(',')) 
    .GroupBy(line => line[0] + "," + line[1]) 
    .Select(group => 
     group.Key + "," + 
     String.Join("-", group.Select(line => line[2]).ToArray()) + "," + 
     String.Join("-", group.Select(line => line[3]).ToArray()) 
    ); 
+0

是否可以根據第2行對行進行排序?如果是1-11-15-2-6,可以重新排列爲1-2-6-11-15? – Mutley 2011-03-11 06:25:56

0

試試這個

var results = from l in File.ReadAllLines(@"..\..\TextFile1.txt") 
       let fields = l.Split(new[] { ',' }) 
       group fields by fields[0] into g 
       select (g.Key + "," + 
       String.Join("-", g.Select(l => l[2]).ToArray()) + 
       "," + 
       String.Join("-", g.Select(l => l[3]).ToArray()));