我試圖從.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; }
}
請幫什麼可以在這種情況下進行。感謝您的幫助。
你正在分裂一個''',但你的示例文本沒有逗號。另外,你需要爲'Input'提供類def。 – Enigmativity
@Enigmativity你好我已粘貼的數據的樣品,並用輸入級認定中 – user5928466
更新問題'功能Loc.'是主鍵? – nozzleman