我有一个包含子对象列表的对象,基本上它是一个有多个部门的机构。可以根据部门的布尔属性禁用或启用部门。当有 1 个或多个部门被禁用时,我想将机构的属性触发为 false。我的部分机构课程中有以下代码:
public bool AllSet
{
get
{
return !(Departments.Where(i => i.Active == false && i.IsDeleted == false).Count() > 0);
}
}
这行得通,但是当我启用它时,我的页面的性能显示机构列表缓慢爬行并且内存使用量激增,显然我在这里做了一些根本错误的事情,有没有人有其他方法可以做到这一点?
工作解决方案
var institutions = from x in ent.Institutions let hasDepartments =
!x.Departments.Any(d => d.Active == false && d.IsDeleted == false) select new {
FullTitle = x.Title + " - " + x.Address.Line1 + ", " + x.Address.City + ", " + x.Address.State, Department = x, AllSet = hasDepartments, Guid = x.Guid
};
instList.DataSource = institutions;