2016-07-22 25 views
0

你好,我似乎无法解决他的问题。我在ASP.Net WebForms应用程序中有一个ListView,我使用Model绑定来使用SelectMethod()获取数据。我有两个模型中定义,下面列出,当连接表时返回查询结果到ListView使用Visual Basic和Linq

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()方法中,我加入t​​blSizes_表,因为我需要抓住从它的一些列数据,我的方法是贴在下面,

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表中获得两个属性,但是当我尝试从tblSizes_表中添加一个属性时,我收到一个错误,说'EDPNum'不是'Ememeber'。 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> 

回答

0

它需要实现一个集团加入检索您的相关藏品

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 
+0

感谢西罗的层次模型,但这并没有解决我的问题,我仍然得到同样的错误加上编写查询,你建议我我正在查询中得到一个InValidCastException。在E.dll中发生类型'System.InvalidCastException'的异常,但未在用户代码中处理 其他信息:无法转换'System.Data.Entity.Infrastructure.DbQuery'1 [VB $ AnonymousType_1'3类型的对象[System.Int32,System.String,System.String]]'键入'System.Linq.IQueryable'1 [E.Models.tblUSSeries]'。 –

+0

道歉,真的我没有用VB.Net ..试试 –

+0

嘿Ciro我改变了之前,你做了你的更改,我知道你是什么意思,通过使用VB,我是一个C#人,但仍然是相同的结果,我知道在ASP MVC中,您可以创建一个具有所需属性的自定义ViewModel类,只需创建它就可以获取我需要的数据。 –