循环后提交更改 - 仅保存最后一条记录

时间:2016-02-10 08:16:01

标签: vb.net loops linq-to-sql submitchanges

以下代码有效,但它只保存循环中的最后一条记录,我无法弄清楚原因。我认为Submitchanges()位于循环结束时的正确位置。有人可以告诉我什么是错的吗?感谢。

Sub POPULATE_CHAIN()

    Dim newChain As New CHAIN
    Dim dpSTRIKE As Integer

    'get list of Options Contracts
    Dim lstOPT = From Z In DATA.OPTIONs, X In DATA.UDLies
                 Where X.UDLY_SYM = Z.UDLY_SYM
                 Select Z.CONTRACT, Z.STRIKE_GAP, X.UDLY_LAST

    Dim dctOPT = lstOPT.ToDictionary(Function(Z) Z.CONTRACT)

    For Each key In dctOPT.Keys

        For COUNT = 1 To 5
            dpSTRIKE = 1850 + 5 * COUNT
            Dim lkup = From Z In DATA.CHAINs
                       Select Z
            Dim RCD_EXISTS As Boolean = lkup.Any(Function(Z) Z.CONTRACT = dctOPT(key).CONTRACT And Z.P_C = "C" And Z.STRIKE = dpSTRIKE)

            If RCD_EXISTS = False Then
                newChain.CONTRACT = dctOPT(key).CONTRACT
                newChain.P_C = "C"
                newChain.STRIKE = dpSTRIKE
                DATA.CHAINs.InsertOnSubmit(newChain)
            Else
                newChain.CONTRACT = dctOPT(key).CONTRACT
                newChain.P_C = "C"
                newChain.STRIKE = dpSTRIKE
            End If

        Next
    Next
    DATA.SubmitChanges()
End Sub

1 个答案:

答案 0 :(得分:1)

^[A-Za-z][A-Za-z0-9_-]*$

应该在Fore Each内部,正好在第二个内部。 由于它是在循环外声明的,因此它将从表中分离并再次附加到表。所以它只会插在最后一行。