我的具有关联的域模型如下:
Project
有很多Task
sTask
有很多User
s
根据 a 的状态User
,我需要更新项目中的一个字段。我的查询如下:
var projects = from project in _data.projects
join task in _data.tasks on project.ProjectID equals task.ProjectID
join user in _data.usersactive on task.TaskID equals user.TaskID
where user.Active == false
select project;
这使我可以获取具有非活动用户的项目。
foreach (Project project in projects)
{
bool needUpdate = false;
foreach (Task task in project.tasks)
{
foreach (User user in task.users)
{
// Depending on state of user
// needUpdate = true;
}
}
// If needUpdate = true
// Do something
}
显然,这段代码会让我:
There is already an open DataReader associated with this Connection which must be closed first.
但是,我在 MySQL 上,因此无法简单地利用MultipleActiveResultSets=True
.
我如何能够急切地加载这些关联并让它们在foreach
循环中访问?