2012-08-04 64 views
-1

這是我迄今(代碼段)動態創建和使用LINQ查詢在VB

ASP.NET標記做了結合ASP-的DataGrid:

<div class="Grid-style"> 
    <asp:GridView ID="dgRequiredAttachment" runat="server" AutoGenerateColumns="false" DataKeyNames="Key"> 
     <Columns> 
     <asp:BoundField HeaderText="Key" datafield="Key" SortExpression="Key" Visible="false"/> 
      <asp:BoundField HeaderText="Value" datafield="Value" SortExpression="Value"/> 
     </Columns> 
    </asp:GridView> 
</div> 

VB。 .NET代碼:

Protected Sub ddlCitizenStatus_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlCitizenStatus.SelectedIndexChanged 
Dim context As New EGrantsModel.Entities 
Dim attachmentType As New EGrantsModel.ATTACHMNTTYPE 

Dim DC As Hashtable = New Hashtable 
Dim orderCopyDesc As String = (From orderCopy In context.ATTACHMNTTYPEs Where orderCopy.ATTACHTYPEID = "1" Select orderCopy.DESCRIPTION).First 
Dim notificationLtrDesc As String = (From notificationLetter In context.ATTACHMNTTYPEs Where notificationLetter.ATTACHTYPEID = "2" Select notificationLetter.DESCRIPTION).First 
Dim citizenListDesc As String = (From citizenList In context.ATTACHMNTTYPEs Where citizenList.ATTACHTYPEID = "3" Select citizenList.DESCRIPTION).First 

DC.Add("1", orderCopyDesc) 
DC.Add("2", notificationLtrDesc) 
DC.Add("3", citizenListDesc) 

dgRequiredAttachment.DataSource = DC 
dgRequiredAttachment.DataBind() 
dgRequiredAttachment.Visible = True 

End Sub 

現在在該行

Dim DC As New Hashtable = New Hashtable 

我想:您可以看到我使用LINQ查詢動態創建HashTable。但是,如果我在Attachment類型表中有超過3個條目,那麼它將根據AttachmenttypeID檢查ATTACHMENTType表中的所有值,並使用循環將項目填充/添加到散列表。

有人可以幫助我嗎?

謝謝

回答

0

通過下面的代碼我解決我的問題

VB代碼

 Dim DC As Hashtable = New Hashtable 
    Dim attachmentTypes = (From attachmentTypeData In context.ATTACHMNTTYPEs Select attachmentTypeData.DESCRIPTION, attachmentTypeData.ATTACHTYPEID) 

     For Each a In attachmentTypes 
     DC.Add(a.ATTACHTYPEID, a.DESCRIPTION) 
     Next 

     dgRequiredAttachment.DataSource = DC 
     dgRequiredAttachment.DataBind() 
     dgRequiredAttachment.Visible = True