我有一个带有gridview的表单,当从GV中选择一个记录时,它调用另一个页面details.aspx,详细信息页面有一个FormView,使用SelectMethod =“GetData”,显示所选id记录的数据,a遵循:
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
在绑定到FormView的数据的同时,我必须在此记录的标签中显示某些数据,但在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
我必须再次查询数据库为IQueryable,才能在返回数据之前选择所需的数据。
我不认为这种方式在同一个地方打两次数据库是切实可行的,即使我不使用Ado.Net也不会这样做,有没有办法增强代码?
感谢您的帮助。
答案 0 :(得分:0)
将查询结果放入临时变量中,如此
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