4

親/子IDのリストがあり、特定の親IDのすべての子IDを取得したいと思います。nullの親はありません(最上位のIDは子IDとして表示されません)。

現在、親/子IDはリストにKeyValuePairとして記録されていますが、それがより良い場合は、これを別のデータ構造に簡単に変更できます。

List<KeyValuePair<int, int>> groups = new List<KeyValuePair<int, int>>();
groups.Add(new KeyValuePair<int,int>(parentID, childID));

たとえば、ここにサンプルの親/子があります。親27の子は、5944、2065、2066、2067、6248、6249、6250になります。

Parent  Child
27      1888
1888    5943
1888    5944
5943    2064
5943    2065
5943    2066
5943    2067
2064    6248
2064    6249
2064    6250

どんな助けでも大歓迎です!

4

2 に答える 2

6

Dictionary<int, List<int>>親がキーで値(intのリスト)が子であるタイプを変更しないのはなぜですか?

次に、以下を使用して子のリストを取得します。

    private List<int> GetAllChildren(int parent)
    {
        List<int> children = new List<int>();
        PopulateChildren(parent, children);
        return children;
    }

    private void PopulateChildren(int parent, List<int> children)
    {
        List<int> myChildren;
        if (myitems.TryGetValue(parent, out myChildren))
        {
            children.AddRange(myChildren);
            foreach (int child in myChildren)
            {
                PopulateChildren(child, children);
            }
        }
    }

パフォーマンスへの影響を重み付けする必要があります。これにより、読み取りが高速化され、書き込みが低速化されます(ほとんどの場合、誰も気付かないでしょう)。

また、を使用してリストが辞書にあるかどうかを確認する必要がありmyitems.TryGet(...)ます。ない場合は、リストを作成する必要がありますが、これはo(1)であるため、実質的に瞬時に実行されます。

private static void AddEntry(int parent, int child)
{
    List<int> children;
    if (!myitems.TryGetValue(parent, out children))
    {
        children = new List<int>();
        myitems[parent] = children;
    }
    children.Add(child);
}
于 2012-06-26T01:18:00.500 に答える
0

簡単だ。次の配列にリストがあると考えてください

    List<KeyValuePair<int, int>> groups = new List<KeyValuePair<int, int>>();
    groups.Add(new KeyValuePair<int, int>(27, 1888));
    groups.Add(new KeyValuePair<int, int>(1888, 5943));
    groups.Add(new KeyValuePair<int, int>(1888, 5944));
    groups.Add(new KeyValuePair<int, int>(5943, 2064));
    groups.Add(new KeyValuePair<int, int>(5943, 2065));
    groups.Add(new KeyValuePair<int, int>(5943, 2066));
    groups.Add(new KeyValuePair<int, int>(5943, 2067));
    groups.Add(new KeyValuePair<int, int>(2064, 6248));
    groups.Add(new KeyValuePair<int, int>(2064, 6249));
    groups.Add(new KeyValuePair<int, int>(2064, 6250));
    groups.Add(new KeyValuePair<int, int>(2000, 1000));
    // Pass the 1st parameter as the parent to get all children
    List<int> childs = GetAllChild(27, groups);

子を動的に取得するには、「再帰関数」を使用する必要があります。次のメソッドを呼び出すだけで、親のすべての子を取得できます

public List<int> GetAllChild(int id,List<KeyValuePair<int, int>> newLst)
{
      List<int> list = new List<int>();
      for (int i = 0; i < newLst.Count; i++)
      {
            if (Convert.ToInt32(newLst[i].Key) == id)
            {
                 if (!list.Contains(Convert.ToInt32(newLst[i].Value)))
                 {
                     list.Add(Convert.ToInt32(newLst[i].Value));
                     List<int> l = GetAllChild(newLst[i].Value, newLst);
                     list.AddRange(l);
                 }
            }
       }
       return list;
}
于 2014-10-15T06:38:16.973 に答える