LINQ到在GridView的SQL对象的子属性属性、对象、LINQ、GridView

2023-09-04 05:34:34 作者:▁▂▃繁花似锦我写不够°

例如,在我的WCF,如果我有一个客户表和订单表是这样一个联合体,以对方客户的ID。

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.

在常规VB code我可以做到这一点:

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)

以上code工作正常,但如果我想从一个GridView中访问一样,它似乎并没有做出同样的反应(albiet,从一个不同的方法,但我申请的同一pricipals到每种方法虽然)

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?

推荐答案

您可以尝试将部分方法Customer类名为定单计数和参考,在您的BoundField。右键单击您的dbml文件,然后进入查看code再加入。

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; }
    }
}