如何优化这段代码?
ParentDoglist,ChildDoglistis - Ilist。dogListBox - 列表框
foreach (Dog ParentDog in ParentDoglist)
{
foreach (Dog ChildDog in ChildDoglist)
{
if(ParentDog.StatusID==ChildDog.StatusID)
dogListBox.Items.Add(new ListItem(ParentDog.Name, ParentDog.Key));
}
}
编辑: ParentDogTypeList、DogTypeList 被重命名为 ParentDoglist、ChildDoglist,两者互不相关
if(ParentDog.Key==ChildDog.Key)
改为
if(ParentDog.StatusID==ChildDog.StatusID)
完整故事:
我需要填充一个下拉菜单,它会回报父子关系。有些狗可能没有孩子,被称为叶狗。我还需要显示该特定类别中的狗数量
DD 看起来像
Parent1
Child11 (10)
Child12 (12)
Parent2
Child21 (23)
Child22 (20)
Leaf1 (20)
Leaf2 (34)
因此,ParentDoglist 会将所有子元素和叶子元素与计数一起带来,而 ChildDogList 将具有父元素和叶子 ID,因此我将能够将相应的子元素填充到它们的父元素并直接绑定叶子。
Parent、Child 和 Leaf Dog 将保存在一个表中,并通过 statusid 进行区分,而 count 将在另一个表中。
没有父母有任何计数,只有孩子和叶子有计数
表架构: