2017-02-09 18 views
0

相關的項目我有下面的類C#如何找到特定項目,並將其從名單

public class Item 
{ 
    public int Id { get; set; } 
    public int ParentId { get; set; } 
    public string Content { get; set; } 
    public bool IsLastItem { get; set; } 
} 

讓說,我有以下的模型,我想找到標識特定項目和它的相關項目的ID。在這種情況下,我想查找item9的Id,它是相關的項目ID。

結果應該包含9,10,11,12,13,14

Model

我從數據庫中獲得我的模型的列表,我模擬它的代碼塊這樣

var items = new List<Item> 
{ 
    new Item 
    { 
     Id = 1, 
     ParentId = 0, 
     Content = "item1", 
     IsLastItem = false 
    }, 
    new Item 
    { 
     Id = 2, 
     ParentId = 1, 
     Content = "item2", 
     IsLastItem = false 
    }, 
    new Item 
    { 
     Id = 3, 
     ParentId = 1, 
     Content = "item3", 
     IsLastItem = true 
    }, 
    new Item 
    { 
     Id = 4, 
     ParentId = 1, 
     Content = "item4", 
     IsLastItem = true 
    }, 
    new Item 
    { 
     Id = 5, 
     ParentId = 2, 
     Content = "item5", 
     IsLastItem = false 
    }, 
    new Item 
    { 
     Id = 6, 
     ParentId = 5, 
     Content = "item6", 
     IsLastItem = false 
    }, 
    new Item 
    { 
     Id = 7, 
     ParentId = 5, 
     Content = "item7", 
     IsLastItem = false 
    }, 
    new Item 
    { 
     Id = 8, 
     ParentId = 6, 
     Content = "item8", 
     IsLastItem = true 
    }, 
    new Item 
    { 
     Id = 9, 
     ParentId = 7, 
     Content = "item9", 
     IsLastItem = false 
    }, 
    new Item 
    { 
     Id = 10, 
     ParentId = 9, 
     Content = "item10", 
     IsLastItem = true 
    }, 
    new Item 
    { 
     Id = 11, 
     ParentId = 9, 
     Content = "item11", 
     IsLastItem = false 
    }, 
    new Item 
    { 
     Id = 12, 
     ParentId = 11, 
     Content = "item12", 
     IsLastItem = true 
    }, 
    new Item 
    { 
     Id = 13, 
     ParentId = 11, 
     Content = "item13", 
     IsLastItem = true 
    }, 
    new Item 
    { 
     Id = 14, 
     ParentId = 11, 
     Content = "item14", 
     IsLastItem = true 
    } 
}; 
+3

如果你想使用你的數據就像一個樹結構然後爲什麼不實際使用樹結構而不是列表? –

+0

@BlakeThingstad:我從數據庫中獲得該模型。這就是爲什麼我把它作爲一個平面模型。 –

+0

如果這些數據被廣泛使用,可能需要考慮將其轉換爲樹結構。這至少值得研究。以下是我通過快速搜索找到的一個示例:[平面數據到分層模型](http://stackoverflow.com/questions/26949442/flat-data-to-hierarchical-model-c-sharp) –

回答

1

你需要一個遞歸的方法

// Start finding the base item to start the recursive search 
Item x = items.Where(i => i.Id == 9).FirstOrDefault(); 
List<Item> found = Search(items, x); 

// Insert the starting item at the first position (Add is also good but...) 
found.Insert(0, x); 



public List<Item> Search(List<Item> list, Item parent) 
{ 
    // Prepare the list to return... 
    List<Item> found = new List<Item>(); 
    if (parent != null) 
    { 
     // Search all the items that have the parent passed 
     List<Item> temp = list.Where(x => x.ParentId == parent.Id).ToList(); 
     // Add the list to the return variable 
     found.AddRange(temp); 
     // For each child of this parent look for their childs 
     foreach(Item x in temp) 
      found.AddRange(Search(list, x)); 
    } 
    return found; 
} 
0

您可以使用LINQ,類似下面應該讓你開始...

var parent = items.First(x => x.Content == "item9"); 
var children = items.Where(x => x.Id == parent.Id); 
+1

請注意,這隻會找到直接的孩子,而不是孩子的孩子。 – Foggzie

+0

@GuntherFox是正確的,我錯過了圖形的那一部分。爲了得到孩子的孩子,你需要實現遞歸方法。這聽起來像數據庫模型可以更新爲使用導航屬性和'.Include()'使這更容易。 –

相關問題