我怎样才能 select 视图中模型的特殊索引?

How can I select special Index of model in View?

我在控制器中使用此查询从连接四个表创建列表:

var ViewModel = (from APP in db1.application
                 where APP.APP_STATUS == "TO_DO"
                 join APPDEL in db1.app_delegation on APP.APP_UID equals APPDEL.APP_UID
                 join Con in db1.content on APPDEL.TAS_UID equals Con.CON_ID
                 join BPMNPRC in db1.bpmn_process on APPDEL.PRO_UID equals BPMNPRC.PRJ_UID
                 join RUSRE in db1.rbac_users on APP.APP_INIT_USER equals RUSRE.USR_UID
                 where APPDEL.TAS_UID == Con.CON_ID 
                    && Con.CON_CATEGORY == ("TAS_TITLE")
                    && APP.APP_UID == APPDEL.APP_UID 
                    && APPDEL.DEL_THREAD_STATUS == "OPEN" 
                    && APPDEL.USR_UID == bpmUseid.BPMSID
                 select new ApplicationContentViewModel
                         {
                             creator = RUSRE,
                             app_delegation = APPDEL,
                             application = APP,
                             content = Con,
                             task = BPMNPRC,
                         }).AsEnumerable();
return View(ViewModel);

但是我需要在APPDEL(app_delegation)中select一条特殊记录,然后检索这条记录的名称。

我在 select 视图中使用此命令按 DEL_INDEX 的字段查看项目:

@foreach (var item in Model)
{
  <th>                                                    
  @(item.app_delegation.DEL_INDEX == Model.Max(x => x.app_delegation.DEL_INDEX) - 1 ? item.creator.USR_LASTNAME : "");
 </th>  
}

事实上,我想selectDEL_INDEX - 1的最大值,并检索它的姓氏。

不幸的是,我得到这个错误:

System.Data.Entity.Core.EntityCommandExecutionException:: 'An error occurred while executing the command definition. See the inner exception for details.'

Inner Exception:

MySqlException: There is already an open DataReader associated with this Connection which must be closed first

我该如何处理这个错误?

我认为您应该在执行表达式 Model.Max(x => x.app_delegation.DEL_INDEX) 之前使用 ToList() 方法而不是 AsEnumerable() 方法,如下所示:

var ViewModel = (from APP in db1.application
                 where APP.APP_STATUS == "TO_DO"
                 join APPDEL in db1.app_delegation on APP.APP_UID equals APPDEL.APP_UID
                 join Con in db1.content on APPDEL.TAS_UID equals Con.CON_ID
                 join BPMNPRC in db1.bpmn_process on APPDEL.PRO_UID equals BPMNPRC.PRJ_UID
                 join RUSRE in db1.rbac_users on APP.APP_INIT_USER equals RUSRE.USR_UID
                 where APPDEL.TAS_UID == Con.CON_ID 
                 && Con.CON_CATEGORY == ("TAS_TITLE")
                 && APP.APP_UID == APPDEL.APP_UID 
                 && APPDEL.DEL_THREAD_STATUS == "OPEN" 
                 && APPDEL.USR_UID == bpmUseid.BPMSID
                 select new ApplicationContentViewModel
                 {
                     creator = RUSRE,
                     app_delegation = APPDEL,
                     application = APP,
                     content = Con,
                     task = BPMNPRC,
                 }).ToList(); // here is the change

说明

通过调用 AsEnumerable(),数据 reader 保持打开状态,当执行 Model.Max() 时,LINQ to Objects 会抛出异常,因为 MySQL 您正在使用的连接器没有尚不支持 MARS(多个活动结果集)。使用 ToList() 方法可确保在延迟加载检查所有导航属性时完成数据 reader 操作。

旁注:

假设您使用的是 EF,请尝试使用 Include() 预先加载,以便在处理多个查询时在单个语句中添加相关对象。

类似问题:

Mysql Linq There is already an open DataReader associated with this Connection which must be closed first