I have a GridView that is bound to a LINQDataSource control that is returning a collection of customers.
Within my DataGrid I need to display the home phone number of a customer, if they have one. The phone numbers of a customer are stored in a separate table with a foreign key pointing to the customer table.
The following binding expression gets me the first phone number for a customer:
<asp:TemplateField HeaderText="LastName" SortExpression="LastName">
<ItemTemplate>
<asp:Label ID="PhoneLabel" runat="server" Text='<%# Eval("Phones[0].PhoneNumber") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
I need to figure out how to get the home phone number specifically (filter based on phone type) and handle the scenario where the customer does not have a home phone in the database. Right now it's throwing an out of range exception if the customer does not have any phone numbers.
I've tried using the Where operator with a lambda expression to filter the phone type but it doesn't work:
<%# Eval("Phones.Where(p => p.PhoneTypeId == 2).PhoneNumber") %>
Solutions or links to any good articles on the subject would be much appreciated.