fARcRY
fARcRY

Reputation: 2358

Binding entities with foreign key to datalist

I have the following code:

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

On the front end I do the following:

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

But I get the following error:

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

Upvotes: 0

Views: 657

Answers (2)

kubal5003
kubal5003

Reputation: 7254

As you are developing asp.net flattening the data is probably the best solution. You may also consider adding a property to your class like

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

}

Upvotes: 0

fARcRY
fARcRY

Reputation: 2358

Found a solution:

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

Upvotes: 1

Related Questions