2012-08-04 26 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