C#如何从列表中查找特定项目及其相关项目

时间:2017-02-09 22:03:49

标签: c# list find tree-traversal

我有以下课程

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

假设我有以下型号,我想通过Id和它的相关项目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
    }
};

2 个答案:

答案 0 :(得分: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;
}

答案 1 :(得分:0)

你可以使用linq,下面的内容应该让你入门......

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