2016-06-21 112 views
0

我有以下數據結構,我想將它轉換爲基於RelationId的層次結構。這是根據RelationId排序的。基於數字序列將對象轉換爲層次結構

id = 2具有relationId = 2並且以下兩行具有realtionId = 0。這代表了ID = 3和Id = 4是Id的子= 2

Id Name RelationId SortOrder 
    1  A  1    1 
    2  B  2    2 
    3  C  0    3 
    4  D  0    4 
    5  E  3    5 
    6  F  0    6 
    7  G  0    7 
    8  H  4    8 

最終結果將是象以下

Id = 1 
    | 
Id = 2 
    |___ Id = 3 , Id = 4 

Id = 5 
    |___ Id= 6 , Id=7 

Id = 8 

期望的結果是如下(爲簡單起見表示它作爲列表) 。這將是在C#中List<Something>

Result = 
[ 
    { Id = 1, Name = A, Children = Null }, 
    { Id = 2, Name = B, Children = [{ Id = 3, Name = C }, {Id = 4, Name = D }] }, 
    { Id = 5, Name = E, Children = [{ Id = 6, Name = F }, {Id = 7, Name = G }] }, 
    { Id = 8, Name = H} 
] 

我不成功的嘗試是如下

var finalResult = new List<sampleDataClass>(); 
var sampleData = GetMeSampleData(); 
var count = sampleData.Count(); 

foreach (var item in sampleData) 
{ 
    var alreadyExist = finalResult.Any(x => x.Id == item.Id); 
    var newObject = new sampleDataClass(); 

    if (!alreadyExist && item.RelationId!= 0) 
    { 
     newObject = item; 
    } 

    for (int i = item.SortOrder; i < count; i++) 
    { 
     if (sampleData[i].RelationId== 0) 
     { 
      newObject.Children.Add(sampleData[i]); 
     } 
    } 

    finalResult.Add(newObject); 
} 
+0

你爲什麼不使'RelationId'點在父的'Id' ?只需使用0或-1或其他值來表示它是頂級父級? – juharr

+0

這是一個排序數組,relationId = 1是頂級 – InTheWorldOfCodingApplications

+0

添加了sortOrder字段以及 – InTheWorldOfCodingApplications

回答

1

由於您RelationId決定它是否是一個根或嵌套元素,可以形成基於這些關係組,做到這一點。我會建議使用的LINQ

List<SomeData> somedata = ... // your data. 

int index=0; 
var results = somedata 
    .Select(x=> new {gid = x.RelationId ==0? index: ++index, item=x}) 
    .GroupBy(x=> x.gid) 
    .Select(x=> { 
     var first = x.FirstOrDefault(); 

     return new 
     { 
      Id = first.item.Id, 
      Name = first.item.Name, 
      Children = x.Skip(1).Select(s=> new { 
       Id = s.item.Id, 
       Name = s.item.Name, 
      })      
     }; 
    }) 
    .ToList(); 

輸出:

Id=1, Name=A 
Id=2, Name=B 
    Id=3, Name=C 
    Id=4, Name=D 
Id=5, Name=E 
    Id=6, Name=F 
    Id=7, Name=G 
Id=8, Name=H 

入住這Working Code

+0

非常感謝。奇蹟般有效 – InTheWorldOfCodingApplications

0

我已經做了這個樣子。不知道是否可以有一些更優雅的解決方案

var data = new List<MyDataObject>(); 
      var SampleData = GetMeSampleData; 
      var count = SampleData.Count(); 
      for (int i=0;i<count;i++) 
      { 
       var rootAdded = false; 
       var relationId = SampleData[i].relationId; 
       var alreadyExist = data.Any(x => x.Id == SampleData[i].Id); 
       var mydataObject = new MyDataObject(); 
       if (!alreadyExist && SampleData[i].RelationId != 0) 
       { 
        mydataObject = SampleData[i]; 
        rootAdded = true; 
       } 


       for(int j=i+1;j<count;j++) 
       { 
        if ((SampleData[j].RelationId == 0 && rootAdded)) 
        { 
         mydataObject.Children.Add(SampleData[j]); 
        } 
        if (SampleData[j].SubjectId != 0) 
         break; 


       } 


       if (rootAdded) 
       { 
        data.Add(mydataObject); 

       } 
相關問題