如何将实体数据源绑定到查询结果

时间:2010-10-10 16:32:51

标签: vb.net entity-framework

我有一个查询将会消失并查找数据

 Dim HSNs As String = String.Join(",", ListOfHSNs.Cast(Of String)().ToArray())



        Dim query As String = "SELECT VALUE O FROM v_BillData AS O WHERE O.HSNumber IN {'" & HSNs & "'}"



        Dim hs As New ObjectQuery(Of v_BillData)(query, CType(Session("ObjectCon"), ObjectContext))

我现在想要做的是使用此查询的结果来数据绑定到EntityDataSource 我怎么能这样做?

1 个答案:

答案 0 :(得分:1)

您可以尝试使用Choose EntityDataSource事件,如以下示例所示:


Protected Sub EntityDataSource1_Selecting(ByVal sender As Object, ByVal e As EntityDataSourceSelectingEventArgs)
  Dim HSNs As String = String.Join(",", ListOfHSNs.Cast(Of String)().ToArray())
  Dim query As String = "SELECT VALUE O FROM v_BillData AS O WHERE O.HSNumber IN {'" & HSNs & "'}"
  Dim source As EntityDataSource = Nothing
  source = TryCast(Me.Page.FindControl("EntityDataSource1"),EntityDataSource)
  If (Not source Is Nothing) Then
    source.EntitySetName = Nothing
    source.CommandText = query
  End If
End Sub

您应该将EntitySetName设置为Nothing,因为如果之前已经设置了EntityDataSource,它将引发错误。