使用转发器,从该表中取出FK并显示列

时间:2013-03-06 11:22:52

标签: asp.net linq

使用转发器,我遍历'SelectedUtility'表并输出匹配的位置。 这样可以正常工作,但它会显示“Utility_Id”,它是实用程序表中“id”的外键。

我实际上想在Utility表中显示'name'列。我怎样才能做到这一点... 类似于<th style="width:200px;"><%# Eval("Utility_Id.name") %></th>的东西 或者是在代码后面完成...感谢任何回复

aspx:

 <asp:Repeater id="rptSelectedUtilities" runat="server">
                <HeaderTemplate>
                    <table class="detailstable FadeOutOnEdit">
                        <tr>   
                            <th style="width:200px;">Utility</th>    
                        </tr>
                </HeaderTemplate>
                <ItemTemplate>
                        <tr>
                            <th style="width:200px;"><%# Eval("Utility_Id") %></th> 
                        </tr>
                </ItemTemplate>
                <FooterTemplate>
                    </table>
                </FooterTemplate>
            </asp:Repeater>

代码背后:

DBData db = new DBDatat();
        List<Select_Utility> SelectedUtility = 
            (from SU in db.Select_Utilities
             where SU.Work_Id == 3
             select SU).ToList();

        rptSelectedUtilities.DataSource = SelectedUtility;

rptSelectedUtilities.DataBind();


编辑:

   DBData db = new DBDatat();
    List<Utility_Company> Utility = new List<Utility_Company>();
    List<Select_Utility> SelectedUtility = 
        (from SU in db.Select_Utilities
         join UTL in Utility on SU equals UTL.id
         where SU.Worksite_Id == 3
         select SU).ToList();

    rptSelectedUtilities.DataSource = SelectedUtility;
    rptSelectedUtilities.DataBind();

引发错误'join子句中某个表达式的类型不正确。调用“join”

时类型推断失败

//连接应该是名称..所以在SU上的实用程序中连接UTL等于UTL.Name //而不是UTL.id

还尝试在SU等于UTL.id的db.Utility_Companies中加入UTL ...同样的错误


第二次编辑:  &lt;%#Eval(“Name”)%&gt; 抛出错误:DataBinding:'Select_Utility'不包含名称为'Name'的属性。

     DBData db = new DBData();

        List<Select_Utility> SelectedUtility = 
            (from SU in db.Select_Utilities
             where SU.Worksite_Id == 3
             select SU).ToList();

        rptSelectedUtilities.DataSource = SelectedUtility;
        rptSelectedUtilities.DataBind();


   List<Utility_Company> Utility = new List<Utility_Company>();
        var query = from u in db.Utility_Companies
                    join su in db.Select_Utilities
                         on u.id equals su.Utility_Id
                    where su.Worksite_Id == 3
                    select u;

        rptSelectedUtilities.DataSource = query.ToList();
        rptSelectedUtilities.DataBind();

2 个答案:

答案 0 :(得分:1)

查询(您应该选择已加入的实用程序):

DBData db = new DBDatat();
var query = from u in db.Utility
            join su in db.Select_Utilities
                 on u.Id equals su.Utility_Id
            where su.Work_Id == 3
            select u;

rptSelectedUtilities.DataSource = query.ToList();
rptSelectedUtilities.DataBind();

页面(显示实用程序名称):

<th style="width:200px;"><%# Eval("Name") %></th>

答案 1 :(得分:1)

使用Inner连接外表。并选择该ID。

DBData db = new DBDatat();
        List<Select_Utility> SelectedUtility = 
            (from SU in db.Select_Utilities join UTL in Utility on SU equals UTL.Utility_Id
             where SU.Work_Id == 3
             select SU).ToList();