Linq to SQL - 当子对象更改时,更新父对象上的计算字段(如“小计”)

时间:2021-03-30 08:22:48

I have a L2S SalesOrder entity with a child set of LineItems. Using winforms, on my Sales Order form I have a datagridview bound to the LineItems and it works like a dream. If the user changes a LineItem quantity (for instance), my LineItem partial class handles the OnQuantityChanged and recalculates the LineItem SubTotal field and the grid is automagically updated. I'm ecstatic with how clean and neat it is.

我有一个L2S SalesOrder实体,其子集为LineItems。使用winforms,在我的销售订单表单上,我有一个绑定到LineItems的datagridview,它就像一个梦想。如果用户更改LineItem数量(例如),我的LineItem分部类将处理OnQuantityChanged并重新计算LineItem SubTotal字段,并自动更新网格。它是多么干净和整洁,我欣喜若狂。

Now I would like to have a calculated field on my SalesOrder object called "Total" that is the total of all the LineItems' SubTotal fields (plus a few other things like tax and shipping, but don't worry about that for now). What is the best/cleanest way for the SalesOrder's Total field to be automagically updated whenever a LineItem is changed? If someone could just point me in the right direction on this I would be most appreciative.

现在我想在SalesOrder对象上有一个名为“Total”的计算字段,它是所有LineItems的SubTotal字段的总和(加上一些其他的东西,如税和运费,但现在不用担心)。每当LineItem发生变化时,SalesOrder的Total字段可以自动更新的最佳/最干净方式是什么?如果有人能指出我正确的方向,我会非常感激。

Don't know if it makes a difference but I'm using Rick Strahl's Linq To SQL business framework (and it is fantastic, BTW!).

不知道它是否有所作为,但我正在使用Rick Strahl的Linq To SQL业务框架(这太棒了,BTW!)。

Thanks!

2 个解决方案

#1


You might want to check out Bindable LINQ:

您可能想查看Bindable LINQ:

Bindable LINQ is a set of extensions to LINQ that add data binding and change propagation capabilities to standard LINQ queries.

Bindable LINQ是LINQ的一组扩展,它为标准LINQ查询添加数据绑定和更改传播功能。

#2


This was actually easier than I thought. I added the ReCalculate() to my SalesOrder and then I can just call it from my LineItem like this...

这实际上比我想象的要容易。我将ReCalculate()添加到我的SalesOrder然后我可以从我的LineItem中调用它,就像这样......

Partial Class SalesOrderLineItem
    Private Sub OnQuantityChanged()
        RefreshMySubTotal()
    End Sub
    Private Sub OnUnitPriceChanged()
        RefreshMySubTotal()
    End Sub
    Private Sub RefreshMySubTotal()
        Dim dNewSubtotal As Decimal = 0
        dNewSubtotal = Me.Quantity * Me.UnitPrice * (1 - (Me.Discount * 0.01))
        If Me.SubTotal <> dNewSubtotal Then
            Me.SubTotal = dNewSubtotal
            If Me.SalesOrder IsNot Nothing Then Me.SalesOrder.ReCalculate()
        End If
    End Sub

#1


You might want to check out Bindable LINQ:

您可能想查看Bindable LINQ:

Bindable LINQ is a set of extensions to LINQ that add data binding and change propagation capabilities to standard LINQ queries.

Bindable LINQ是LINQ的一组扩展,它为标准LINQ查询添加数据绑定和更改传播功能。

#2


This was actually easier than I thought. I added the ReCalculate() to my SalesOrder and then I can just call it from my LineItem like this...

这实际上比我想象的要容易。我将ReCalculate()添加到我的SalesOrder然后我可以从我的LineItem中调用它,就像这样......

Partial Class SalesOrderLineItem
    Private Sub OnQuantityChanged()
        RefreshMySubTotal()
    End Sub
    Private Sub OnUnitPriceChanged()
        RefreshMySubTotal()
    End Sub
    Private Sub RefreshMySubTotal()
        Dim dNewSubtotal As Decimal = 0
        dNewSubtotal = Me.Quantity * Me.UnitPrice * (1 - (Me.Discount * 0.01))
        If Me.SubTotal <> dNewSubtotal Then
            Me.SubTotal = dNewSubtotal
            If Me.SalesOrder IsNot Nothing Then Me.SalesOrder.ReCalculate()
        End If
    End Sub