2013-12-21 35 views
3

我真的很新的ASP.NET和我已經看了無數的教程,試圖弄清楚這一點,但似乎無法讓我的頭周圍。我可以從tbProject成功輸出記錄使用ListView和實體框架,但代替refDepartmentID,我想從refDepartment表顯示refDepartmentValue。這是一個快速表結構。使用ListView控件來顯示數據,但加入一個參考表

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(); 
} 

任何幫助,將不勝感激。謝謝!

回答

3

聲明,我這樣做是在我的頭上沒有Visual Studio的,所以它可能有一個小語法錯誤。讓我知道如果它。

你需要做兩件事情,首先你必須加入你的tbProject使用,內部(或外部)加入需要根據您的期望的行爲refDepartments的entites。一,你在LINQ加入了兩個表,要創建一個使用「選擇新的」運算符,它會創建一個新匿名類。這個新類的成員將根據從中選擇的實體的數據類型創建。

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(); 
} 
+0

謝謝真是棒極了! – user1431633

相關問題