使用ListView控件显示数据但加入引用表

时间:2013-12-21 19:15:38

标签: c# asp.net linq entity-framework listview

我是ASP.NET的新手,我看过无数的教程试图解决这个问题,但似乎无法理解它。我可以使用ListView和实体框架从tbProject成功输出记录,但是我想从refDepartment表中显示refDepartmentValue而不是refDepartmentID。这是一个快速的表结构。

tbProject
-ProjectID
-refDepartmentID
-ProjectName

refDepartment
-refDepartmentID
-refDeparmentValue

我可以在SQL中写这个没有JOIN的问题,但我不知道我把它放在哪里。我已经玩了一些LINQ,看起来这就是我要完成的目标。也许在代码隐藏文件中编写LINQ然后将其绑定到ListView控件,也许?

这是ListView控件:

<asp:ListView runat="server" 
  DataSourceID="EntityDataSource1">



  <ItemTemplate>
    <tr style="">
      <td>
        <asp:Label ID="ProjectIDLabel" runat="server" Text='<%# Eval("ProjectID") %>' />
      </td>
      <td>
        <asp:Label ID="refDepartmentIDLabel" runat="server" 
         Text='<%# Eval("refDepartmentID") %>' />
      </td>

    </tr>
   </ItemTemplate>
   <LayoutTemplate>
   <table runat="server">
     <tr runat="server">
       <td runat="server">
         <table ID="itemPlaceholderContainer" runat="server" border="0" style="">
           <tr runat="server" style="">
             <th runat="server">
               ProjectID</th>
             <th runat="server">
               refDepartmentID</th>
            </tr>
            <tr ID="itemPlaceholder" runat="server">
            </tr>
          </table>
        </td>
      </tr>
      <tr runat="server">
        <td runat="server" style="">
        </td>
      </tr>
     </table>
    </LayoutTemplate>
   </asp:ListView>

以下是我使用EF填充下拉列表的方式,以便您至少可以参考命名的方式。

// populates Departments dropdownlist
using (dbOrganizationEntities1 myEntities = new dbOrganizationEntities1())
{
    var allDepartments = from refDepartments in myEntities.refDepartments
                         select refDepartments;
    ddlDepartments.DataSource = allDepartments;
    ddlDepartments.DataValueField = "refDepartmentID";
    ddlDepartments.DataTextField = "refDepartmentValue";
    ddlDepartments.DataBind();
}

任何帮助将不胜感激。谢谢!

1 个答案:

答案 0 :(得分:3)

免责声明,我在没有Visual Studio的情况下完成了这项工作,因此可能会出现轻微的语法错误。如果有,请告诉我。

您需要做两件事,首先,您必须根据需要使用内部(或外部)连接将您的tbProject加入refDepartments Entites,具体取决于您所需的行为。你有两个在LINQ中加入的表,你想使用“select new”运算符创建一个新的匿名类来创建它。将根据从中选择实体的数据类型创建此新类的成员。

using (dbOrganizationEntities1 myEntities = new dbOrganizationEntities1())
{
    var allDepartments = (from tbProject in myEntities.tbProjects
                            // inner join to department lookup table
                            from refDepartments in myEntities.refDepartments.Where(x=>x.refDepartmentID == tbProject.refDepartmentID) // to do a left join instead of an inner, append .DefaultIfEmpty() after this where clause
                            // select new anon type
                            select new {
                                refDepartmentID = tbProject.refDepartmentID,
                                ProjectName = tbProject.ProjectName,
                                refDepartmentValue = refDepartments.refDepartmentValue,
                            }).ToList(); // I chose to turn the result into a list to demonstrate something below, you can leave it as an enumerable.


    // you can access the properties of the anon type like so
    System.Diagnostics.Debug.Print(allDepartments[0].refDepartmentID);
    System.Diagnostics.Debug.Print(allDepartments[0].refDepartmentValue);

    // bind to your listview, make sure control name is accurate and ItemTemplates are defined for each data column.
    MyListView.DataSource = allDepartments;
    MyListView.DataBind();
}