2017-01-18 158 views
-1

我試圖從.csv文件建立層次結構,它由ID(功能位置)的,說明&父ID(SupFunctLoc)這些都是字符串。我已將數據提取到列表中。 代碼&數據參考樣本。遞歸親子關係C#

Functional Loc. Description SupFunctLoc. 
70003 ABC AS002 
70C2 ABC 70003 
70C2.01 ABC 70C2 
70C2.01.02 ABC 70C2.01 
70C2.01.02.10 ABC 70C2.01.02 
70C2.01.02.10-BG010 ABC 70C2.01.02.10 

示例代碼:

static void Main(string[] args) 
    { 
     List<Input> inputList = new List<Input>(); 
     var yourData = File.ReadAllLines(locate) 
       .Skip(1) 
       .Select(x => x.Split(',')) 
       .Select(x => new Input() 
       { 
        FunctionalLocation = x[0], 
        Description = x[1], 
        SuppFunctionalLocation = x[2], 

       }); 
     //try 3 

     //try 2 

     var outputList = yourData 
      .Where(i => i.SuppFunctionalLocation!= null) // Just get the parents 
      .Select(i => new Input() 
      { 
       Description = i.Description, 
       SuppFunctionalLocation = i.SuppFunctionalLocation, 
       Children = inputList 
        .Where(x => x.FunctionalLocation.ToString() == i.SuppFunctionalLocation.ToString()) 
        .Select(x => new Input() 
        { 
         Description = x.Description, 
         SuppFunctionalLocation = x.SuppFunctionalLocation, 
         FunctionalLocation = x.FunctionalLocation, 
        }).ToList() 
       }).ToList(); 


     foreach (var output in outputList) 
     { 
      Console.WriteLine(output.Description); 
      output.Children.ForEach(c => Console.WriteLine($"\t {c.Description}")); 
     } 
} 

類認定中輸入

class Input 
    { 
     public string FunctionalLocation { get; set; } 
     public string Description { get; set; } 
     public string SuppFunctionalLocation { get; set; } 
     public List<Input> Children { get; set; } 
    } 

請幫什麼可以在這種情況下進行。感謝您的幫助。

+3

你正在分裂一個''',但你的示例文本沒有逗號。另外,你需要爲'Input'提供類def。 – Enigmativity

+0

@Enigmativity你好我已粘貼的數據的樣品,並用輸入級認定中 – user5928466

+0

更新問題'功能Loc.'是主鍵? – nozzleman

回答

0

試試這個:

var source = @"Functional Loc. Description SupFunctLoc. 
70003 ABC AS002 
70C2 ABC 70003 
70C2.01 ABC 70C2 
70C2.01.02 ABC 70C2.01 
70C2.01.02.10 ABC 70C2.01.02 
70C2.01.02.10-BG010 ABC 70C2.01.02.10"; 

var lines = 
    source 
     .Split(Environment.NewLine.ToCharArray(), StringSplitOptions.RemoveEmptyEntries) 
     .Select(x => x.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)); 

var results = 
    lines 
     .Skip(1) 
     .Select(x => new Input() 
     { 
      FunctionalLocation = x[0], 
      Description = x[1], 
      SuppFunctionalLocation = x[2], 
     }); 

var lookup = results.ToLookup(x => x.SuppFunctionalLocation); 

Func<string, List<Input>> build = null; 
build = SuppFunctionalLocation => 
    lookup[SuppFunctionalLocation] 
     .Select(x => new Input() 
     { 
      FunctionalLocation = x.FunctionalLocation, 
      Description = x.Description, 
      SuppFunctionalLocation = x.SuppFunctionalLocation, 
      Children = build(x.FunctionalLocation), 
     }) 
     .ToList(); 

List<Input> tree = build("AS002"); 
+0

謝謝你的幫助:) – user5928466

0

從現有父關鍵是相對容易的項目建設層次。我從你的示例中抽象出一些,但它基本上是一樣的。

該項目擬投入層次

public class MyItem 
{ 
    public string Id { get; set; } 
    public string Description { get; set; } 
    public string ParentId { get; set; } 
    // to be filled 
    public IList<MyItem> Children { get; set; } 
} 

的代碼來構建子表。

// assumption: MyItem elements have all properties set except the children collection 
ICollection<MyItem> items = GetMyItems(); 

var parentRelation = items.ToLookup(x => x.ParentId); 
foreach (var item in items) 
{ 
    item.Children = parentRelation[item.Id].ToList(); 
} 
+0

非常感謝...... :) – user5928466

1

一旦您已經閱讀所有的物品放入收集建立與

foreach (var parent in inputlist) 
{ 
    parent.Children = inputlist 
     .Where(child => child.SuppFunctionalLocation == parent.FunctionalLocation) 
     .ToList(); 
} 

父關係,只得到你必須尋找所有的根元素項目,有沒有父

var outputlist = inputlist 
    .Where(child => !inputlist.Any(parent => parent.FunctionalLocation == child.SuppFunctionalLocation)) 
    .ToList();