Linq to SQL object child properties in GridView

2019-06-04 13:41发布

For example, in my WCF, if I have a Customer table and an Order table that have an association to each other on Customer ID.

I want to get the Count of the number of Orders for each Customer.

In regular VB code I can just do this:

Dim CustID As Integer = 1234
Dim cust As New MyService.Customer()
cust = cust.GetCustomer(CustID)
Response.Write(cust.Orders.Count)

The above code works fine but if I want to access the same from within a GridView, it doesn't seem to react the same way (albiet, from a different method but I'm applying the same pricipals to each method though)

<asp:ObjectDataSource ID="odsCustomers" runat="server" SelectMethod="GetCustomers" TypeName="MyService.Customer" />

<asp:GridView ...>
...
<Columns>
    <asp:BoundField DataField="Orders.Count" HeaderText="Order Count" />
</Columns>
...
</asp:GridView>

So how could I do something like this?

1条回答
Explosion°爆炸
2楼-- · 2019-06-04 14:31

You could try adding a partial method to the Customer class called OrderCount and reference that in your BoundField. Right click you dbml file and go to View Code then add.

VB

Partial Public Class Customer

    Public ReadOnly Property OrderCount As Integer
        Get
            Return Me.Orders.Count
        End Get
    End Property

End Class

C#

public partial class Customer
{
    
    public int OrderCount {
        get { return this.Orders.Count; }
    }
}
查看更多
登录 后发表回答