我的关联域名模型如下:
Project
有很多Task
s Task
有很多User
s 根据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
循环中访问它们?
答案 0 :(得分:1)
急切加载linq集:
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).ToList();