如何优化这段代码?
ParentDogList,ChildDogList是 IList,dogListBox 是列表框。
因此,ParentDoglist会带来所有的Child和Leaf元素以及计数,而ChildDogList会有Parent和Leaf ID,因此我将能够将各自的Child填充到其Parent中并直接绑定Leaf。
Parent、Child和Leaf Dog将在一个表中维护,并通过statusid进行区分,计数将在另一个表中。
没有任何父级会有任何计数,只有子级和叶子级会有计数。
表模式如下图所示:
ParentDogList,ChildDogList是 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)
完整内容:
我需要填充下拉列表,其中包含父子关系。有些狗可能没有任何子代,这将被称为叶子狗。我还需要显示该特定类别中狗的数量。
下拉列表应如下所示:
Parent1
Child11 (10)
Child12 (12)
Parent2
Child21 (23)
Child22 (20)
Leaf1 (20)
Leaf2 (34)
因此,ParentDoglist会带来所有的Child和Leaf元素以及计数,而ChildDogList会有Parent和Leaf ID,因此我将能够将各自的Child填充到其Parent中并直接绑定Leaf。
Parent、Child和Leaf Dog将在一个表中维护,并通过statusid进行区分,计数将在另一个表中。
没有任何父级会有任何计数,只有子级和叶子级会有计数。
表模式如下图所示: