2009-10-19 64 views
0

我有以下代码:与外键绑定实体DataList控件

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> 

,但我得到了以下错误:

does not contain a property with the name 'GroupName'.

+0

我不确定。我认为这是EF的问题:(不是因为你的代码。 – anishMarokey 2009-10-19 10:38:53

回答

1

找到了解决办法:

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

由于您正在开发asp.net flattening数据可能是最好的解决方案。你也可以考虑增加一个属性到你的班级,如

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

}