根据上面的评论,我将假设以下内容:
public class Manager {
public Int32 ManagerId { get; set; }
public String ManagerName { get; set; }
}
public class Designer {
public Int32 DesignerId { get; set; }
public Int32 ManagerId { get; set; }
public String DesignerName { get; set; }
}
假设给定设计师的经理是静态的,我将修改经理类:
public class Manager {
private List<Designer> _designers = new List<Designer>();
public Int32 ManagerId { get; set; }
public String ManagerName { get; set; }
public IList<Designer> Designers { get { return _designers; } }
}
然后将管理器存储在KeyedCollection的子类中:
using System.Collections.ObjectModel;
public class ManagerList : KeyedCollection<Int32, Manager> {
protected override int GetKeyForItem(Manager item)
{
return item.ManagerId;
}
}
// Somewhere else
var managerList = new ManagerList();
var someManager = new Manager { ManagerId = 1, ManagerName="Bill Lundberg" };
managerList.Add(someManager);
foreach (var designer in myDesigners)
managerList[designer.ManagerId].Designers.Add(designer);
当然,这一切都没有考虑到设计师管理方面的任何变化。这就是我问的原因:-)