C#中子项的嵌套foreach

Jac*_*els 0 c# foreach nested

我想将数据库中的数据作为项目和子项目添加到字典中。当我有一本按项目和子项目排序的字典时,我将能够根据需要将这本字典添加到组合框中。我有一个这样的数据库结构:

id      item            slug            parent_id
--------------------------------------------------
1       Audi            audi            0
2       BMW             bmw             0
3       Ferrari         ferrari         0
4       A3              a3              1
5       A4              a4              1
6       A5              a5              1
7       A6              a6              1
8       Mercedes-Benz   mercedes-benz   0
9       CLA             cla             8
10      CLS             cls             8
11      C Series        c-series        8
12      S Series        s-series        8
13      SLS AMG         sls-amg         8
14      Mitsubishi      mitsubishi      0
15      Porsche         porsche         0
16      Skoda           skoda           0
17      Tesla           tesla           0
18      A7              a7              1
19      Q3              q3              1
20      SL              sl              8
21      300             300             8
22      Toyota          toyota          0
23      Volkswagen      volkswagen      0
24      A8              a8              1
25      Q2              q2              1
26      Volvo           volvo           0
27      Q5              q5              1
28      Q7              q7              1
29      RS              rs              1
30      80 Series       80-series       1
Run Code Online (Sandbox Code Playgroud)

在这个数据库结构中,我想将数据获取到一个字典中,该字典将使用 teqel 将子项位于其父项下。这是简单的架构:

Audi
    - 80 Series
    - A3
    - A4
    - A5
    - A6
    - A7
    - A8
    - Q2
    - Q3
    - Q5
    - Q7
    - RS
BMW
Ferrari
Mercedes-Benz
    - 300
    - CLA
    - CLS
    - C Series
    - S Series
    - SL
    - SLS AMG
Mitsubishi
Porsche
Skoda
Tesla
Toyota
Volkswagen
Volvo
Run Code Online (Sandbox Code Playgroud)

为了实现这一点,我编码如下:

var listed_items = new Dictionary<int, string> { { 0, "--- Select item ---" } };
foreach (var it in _db.Items.Where(x => x.parent_id == 0))
{
    listed_items.Add(Convert.ToInt32(it.id), it.item);

    foreach (var sub_it in _db.Items.Where(x => x.parent_id > 0))
    {
        if (it.id == sub_id.parent_id)
        {
            listed_items.Add(Convert.ToInt32(sub_id.id), "   - " + sub_it.item);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

但子项不位于父项下。

Zae*_*man 5

如果要按父 ID 将它们分组,则

var dictionary = _db.Items
                    .GroupBy(x => x.parent_id)
                    .ToDictionary(x => x.Key, y => y.ToList());
Run Code Online (Sandbox Code Playgroud)

将所有项目放入Dictionary<int, List<dbItemTypeName>>键为 parentID 的 a 中,并且列表包含具有该 parentID 的所有项目。

如果您想一次将所有项目添加到组合框,则字典不是您的最佳选择,因为它不能保证它会保持顺序。您可能想要使用 SortedDictionary。

var dictionary = _db.Items
                    .OrderBy(x => x.parent_id == 0 ? x.id : x.parent_id)
                    .ThenBy(x => x.parent_id)
                    .ToDictionary(x => x.id, y => y);
var sortedDictionary = new SortedDictionary<int,DBItemType>(dictionary);
Run Code Online (Sandbox Code Playgroud)

其中DBItemType是 中包含的项目的类型_db。如果您想知道这里发生了什么 - 我们首先按结果排序,x.parent_id == 0 ? x.id : x.parent_id因为它将父项与子项分组(id如果parent_id值为 0,则使用常规项),然后按以下顺序排序parent_id以将父项置于顶部(它parent_id是 0)。

如果您想选择项目的字符串表示,而不是项目本身:

var dictionary = _db.Items
                    .OrderBy(x => x.parent_id == 0 ? x.id : x.parent_id)
                    .ThenBy(x => x.parent_id)
                    .ToDictionary(x => x.id, y => y.parent_id == 0 ? y.item : $"   -{y.item}");
var sortedDictionary = new SortedDictionary<int,string>(dictionary);
Run Code Online (Sandbox Code Playgroud)

然后,一定要设定DisplayMember的属性ComboBox"Value"ValueMember财产"Key"