0

我的具有关联的域模型如下:

  • Project有很多Tasks
  • Task有很多Users

根据 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循环中访问?

4

1 回答 1

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();
于 2012-06-04T05:50:31.830 回答