Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/svg/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Entity framework 如何增强此代码(LINQ到实体)_Entity Framework - Fatal编程技术网

Entity framework 如何增强此代码(LINQ到实体)

Entity framework 如何增强此代码(LINQ到实体),entity-framework,Entity Framework,我有一个带有gridview的表单,当从GV中选择一条记录时,它会调用另一个页面details.aspx,details页面有一个带有FormView的表单,使用SelectMethod=“GetData”,显示所选id记录的数据,如下所示: Public Function GetData( ByVal id? As Integer) As Ems_Candidate Try // store record id in memVar to

我有一个带有gridview的表单,当从GV中选择一条记录时,它会调用另一个页面details.aspx,details页面有一个带有FormView的表单,使用SelectMethod=“GetData”,显示所选id记录的数据,如下所示:

Public Function GetData( ByVal id? As Integer) As Ems_Candidate Try // store record id in memVar to use it when retreve the image strRecNo = id.ToString Call LoadCandidateImage() Return _db.DbSet_Candidates.Find(id) Catch ex As Exception ModelState.AddModelError("ModelError", ex.Message.ToString) Return 1 End Try End Function 公共函数GetData(ByVal id?作为整数)作为Ems_候选者 尝试 //将记录id存储在memVar中,以便在检索图像时使用它 strRecNo=id.ToString 调用LoadCandidateImage() Return _db.DbSet_Candidates.Find(id) 特例 ModelState.AddModelError(“ModelError”,例如Message.ToString) 返回1 结束尝试 端函数 在绑定到FormView的数据必须在此记录的标签中显示一些数据的同时,我必须将上述方法更新为以下内容:

Public Function GetData( ByVal id? As Integer) As Ems_Candidate Try // store record id in memVar to use it when retrieve the image strRecNo = id.ToString Call LoadCandidateImage() // Start: new lines added Dim objQuery As IQueryable(Of Ems_Candidate) = _ From c In _db.DbSet_Candidates Where c.CandidateID = id Select c For Each c In objQuery If c.CandidateStatus.Equals("A") Then Me.lblCandidateStatus.Text = "Active" ElseIf c.CandidateStatus.Equals("D") Then Me.lblCandidateStatus.Text = "Deleted" ElseIf c.CandidateStatus.Equals("W") Then Me.lblCandidateStatus.Text = "Waived" Else Me.lblCandidateStatus.Text = "N/A" End If Next // End: new lines added Return _db.DbSet_Candidates.Find(id) Catch ex As Exception ModelState.AddModelError("ModelError", ex.Message.ToString) Return 1 End Try End Function 公共函数GetData(ByVal id?作为整数)作为Ems_候选者 尝试 //将记录id存储在memVar中,以便在检索图像时使用它 strRecNo=id.ToString 调用LoadCandidateImage() //开始:添加新行 Dim objQuery作为IQueryable(Ems_候选者的)=_ 来自c In_db.DbSet_候选者 其中c.CandidateID=id 选择c 对于objQuery中的每个c 如果c.CandidateStatus.等于(“A”),则 Me.lblCandidateStatus.Text=“活动” 如果c.CandidateStatus等于(“D”),那么 Me.lblCandidateStatus.Text=“已删除” 如果c.CandidateStatus.等于(“W”),则 Me.lblCandidateStatus.Text=“放弃” 其他的 Me.lblCandidateStatus.Text=“不适用” 如果结束 下一个 //结束:添加了新的行 Return _db.DbSet_Candidates.Find(id) 特例 ModelState.AddModelError(“ModelError”,例如Message.ToString) 返回1 结束尝试 端函数 我必须再次查询数据库,因为我能够在返回数据之前选择所需的数据

我不认为这种方法在同一个地方两次访问数据库是可行的,即使我使用Ado.Net也不会这样做,有没有办法增强代码


谢谢您的帮助。

将查询结果放入一个临时变量,如下所示

Public Function GetData(ByVal id? As Integer) As Ems_Candidate
    Try
        // store record id in memVar to use it when retreve the image
        strRecNo = id.ToString

        Call LoadCandidateImage()

        Dim result As Ems_Candidate
        result = _db.DbSet_Candidates.Find(id)

        Select Case result.CandidateStatus
            Case "A"
                Me.lblCandidateStatus.Text = "Active"
            Case "D"
                Me.lblCandidateStatus.Text = "Deleted"
            Case "W"
                Me.lblCandidateStatus.Text = "Waived"
            Case Else
                Me.lblCandidateStatus.Text = "N/A"
        End Select

        Return result

    Catch ex As Exception
        ModelState.AddModelError("ModelError", ex.Message.ToString)
        Return 1
    End Try
End Function