LINQ到在GridView的SQL对象的子属性 [英] Linq to SQL object child properties in GridView

查看:107
本文介绍了LINQ到在GridView的SQL对象的子属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,在我的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; }
    }
}

这篇关于LINQ到在GridView的SQL对象的子属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆