使用 Visual Basic 和 Linq 连接表时将查询结果返回到 ListView

Returning a Query Result to an ListView when joining tables Using Visual Basic and Linq

您好,我似乎无法解决他的问题。我在 ASP.Net WebForms 应用程序中有一个 ListView,我正在使用模型绑定通过 SelectMethod() 获取数据。我定义了两个模型,如下所示,

Imports System.ComponentModel.DataAnnotations
Namespace Models
Public Class tblSizes_
    <Key>
    Public Property Series() As Integer
    Public Property Description() As String
    Public Property EDPNum() As String
    Public Overridable Property tblUSSeries() As ICollection(Of tblUSSeries)
End Class
End Namespace

Imports System.ComponentModel.DataAnnotations
Namespace Models
Public Class tblUSSeries
    <Key>
    Public Property Series() As Integer
    Public Property Description() As String
    Public Overridable Property tblSizes_() As ICollection(Of tblSizes_)
End Class
End Namespace

在我的 GetSeries() 方法中,我加入了 tblSizes_ table 因为我需要从其中的某些列中获取数据,我的方法贴在下面,

Public Function GetSeries(<QueryString("id")> searchString As String) As IQueryable(Of tblUSSeries)
    searchLabel.Text = searchString
    Dim pdb = New ProductDataContext()             
    Dim query As IQueryable(Of tblUSSeries) = 
                From a In pdb.tblUSSeries
                Join b In pdb.tblSizes_ On a.Series Equals b.Series
                Where a.Series.ToString().Equals(searchString) Or b.EDPNum.ToString().Equals(searchString)
                Select a
    Return query
End Function

我的问题是,在视图中我可以从 tblUSSeries table 中获取两个属性,但是当我尝试从 tblSizes_ table 添加一个 属性 时,我得到一个错误说 'EDPNum' 不是 'E.Model.tblUSSeries'

的成员
<asp:ListView ID="seriesList" runat="server" DataKeyNames="Series" GroupItemCount="1" ItemType="E.Models.tblUSSeries" GroupPlaceholderID="groupPlaceholder1" ItemPlaceholderID="itemPlaceholder1" SelectMethod="GetSeries">
                        <EmptyDataTemplate>
                            <table id="emptyDataTable" runat="server">
                                <tr>
                                    <td>No data was returned.</td>
                                </tr>
                            </table>
                        </EmptyDataTemplate>
                        <EmptyItemTemplate>
                            <td id="emptyItemTable" runat="server" />
                        </EmptyItemTemplate>
                        <GroupTemplate>
                            <div style="clear: both">
                                <div id="itemPlaceholder1" runat="server">
                                </div>
                            </div>
                        </GroupTemplate>
                        <ItemTemplate>
                            <div style="width: 100%">
                                <div style="font-size: 12px; float: left; width: 50px; padding-bottom: 10px; color: green">
                                    <%#:Item.Series%>
                                </div>
                                <div style="font-size: 12px; font-family: Arial;  float: left; padding-right: 10px">
                                    <div >
                                        <%#:Item.Description%>
                                       </div>
                                </div>
                                <div style="font-size: 12px; font-family: Arial;  float: left; padding-right: 10px">
                                    <div >
                                        <%#:Item.EDPNum%>
                                    </div>
                                </div>
                            </div>
                        </ItemTemplate>

它需要实现一个 Group Join 来检索你的相关集合的层次模型

Public Function GetSeries(<QueryString("id")> searchString As String) As IQueryable(Of tblUSSeries)
    searchLabel.Text = searchString
    Dim pdb = New ProductDataContext()             
     Dim query As IQueryable(Of tblUSSeries) = 
            (From a In tblUSSeries
             Group Join b In tblSizes_ On a.Series Equals b.Series Into jTblSizes = Group
             Where a.Series.ToString().Equals(searchString) Or jTblSizes.Where(Function(p) p.EDPNum.ToString().Equals(searchString)).Any()
             Select New tblUSSeries With {.Series = a.Series, .Description = a.Description, .tblSizes_ = jTblSizes}
            ).AsQueryable().Cast(Of tblUSSeries)
    Return query
End Function