将具有外键的实体绑定到datalist

时间:2009-10-19 10:09:19

标签: asp.net entity-framework

我有以下代码:

var devices = from d in ctx.Devices.Include("DeviceGroups")
    where d.DeviceEnabled == true
    select d;
dlTerminals.DataSource = devices;

在前端,我执行以下操作:

<asp:DataList ID="dlTerminals" runat="server" DataKeyField="DeviceId" GridLines="None" RepeatColumns="2" RepeatDirection="Horizontal" Width="100%">
    <ItemTemplate>
            <%# Eval("DeviceGroups.GroupName")%>
    </ItemTemplate>
</asp:DataList>

但是我收到以下错误:

  

不包含名称为“GroupName”的属性。

2 个答案:

答案 0 :(得分:1)

找到解决方案:

select new { d.DeviceId, d.MAC, d.DeviceType, d.LastConnectTime, d.DeviceGroups.FirstOrDefault().GroupName };

答案 1 :(得分:0)

在开发asp.net展平时,数据可能是最好的解决方案。您也可以考虑在类中添加属性,如

public string DeviceGroupName
{
    get
    {
        return this.DeviceGroups.FirstOrDefault();
    }

}