Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vb.net Linq总是返回最后一条记录_Vb.net_Linq - Fatal编程技术网

Vb.net Linq总是返回最后一条记录

Vb.net Linq总是返回最后一条记录,vb.net,linq,Vb.net,Linq,我成功地获得了记录列表,但问题是最后一条记录总是覆盖第一条记录。记录计数已正确。这是我的密码。请帮忙。谢谢 Private Function MyFunction(ByVal myList As List(Of TT_GENERAL_CONFIGURATION)) Dim pConfig As New Config Dim lst As New List(Of Config) For Each MyCls As TT_GENERAL_CONFIG

我成功地获得了记录列表,但问题是最后一条记录总是覆盖第一条记录。记录计数已正确。这是我的密码。请帮忙。谢谢

Private Function MyFunction(ByVal myList As List(Of TT_GENERAL_CONFIGURATION))
        Dim pConfig As New Config
        Dim lst As New List(Of Config)

        For Each MyCls As TT_GENERAL_CONFIGURATION In myList
            pConfig.ConfigID = MyCls.INTERNAL_NUM
            pConfig.ConfigType = MyCls.CONFIG_TYPE
            pConfig.ConfigName = MyCls.CONFIG_NAME
            pConfig.ConfigValue = MyCls.CONFIG_VALUE

            lst.Add(pConfig)
        Next

        Return lst
End Function

目前,您创建了一个配置实例,并多次将其添加到列表中。在每个循环中,您仅修改此单个配置对象的值。在循环内移动配置创建:

Private Function MyFunction(ByVal myList As List(Of TT_GENERAL_CONFIGURATION)) 
    Dim lst As New List(Of Config)

    For Each MyCls As TT_GENERAL_CONFIGURATION In myList
        Dim pConfig As New Config
        pConfig.ConfigID = MyCls.INTERNAL_NUM
        pConfig.ConfigType = MyCls.CONFIG_TYPE
        pConfig.ConfigName = MyCls.CONFIG_NAME
        pConfig.ConfigValue = MyCls.CONFIG_VALUE

        lst.Add(pConfig)
    Next

    Return lst
End Function
Private Function MyFunction(ByVal myList As List(Of TT_GENERAL_CONFIGURATION))
        Dim pConfig As Config
        Dim lst As New List(Of Config)

        For Each MyCls As TT_GENERAL_CONFIGURATION In myList
            pConfig = New Config()

            pConfig.ConfigID = MyCls.INTERNAL_NUM
            pConfig.ConfigType = MyCls.CONFIG_TYPE
            pConfig.ConfigName = MyCls.CONFIG_NAME
            pConfig.ConfigValue = MyCls.CONFIG_VALUE

            lst.Add(pConfig)
        Next

        Return lst
End Function

这是因为您正在重用对对象的同一引用,这意味着您实际上并没有添加新对象,而是向同一对象添加新引用。您的代码应该如下所示(“Config”类型的初始化在循环中移动):


希望这有帮助。

您只实例化了一次
pConfig
,然后在循环中反复修改它,并在循环中移动实例化EIT