Linq返回结果列表

时间:2013-12-04 09:43:34

标签: vb.net linq

嗨,我坚持将linq结果作为列表返回。我尝试转换但未能返回列表。以下是我的代码。请帮忙。感谢

Public Function GetConfigList(ByVal aConfig As TT_GENERAL_CONFIGURATION) As List(Of Config)
    Dim Data = From p In Db.TT_GENERAL_CONFIGURATION _
               Select p

    If Data IsNot Nothing Then
        ConvertGeneralConfig(Data)
    Else
        Return Nothing
    End If
End Function

Private Function ConvertGeneralConfig(ByVal aConfig As TT_GENERAL_CONFIGURATION) As List(Of Config)

        Dim pConfig As New Config
        pConfig.ConfigID = aConfig.INTERNAL_NUM
        pConfig.ConfigType = aConfig.CONFIG_TYPE
        pConfig.ConfigName = aConfig.CONFIG_NAME
        pConfig.ConfigValue = aConfig.CONFIG_VALUE

        Return pConfig
    End Function

2 个答案:

答案 0 :(得分:0)

如果您只想要一个结果 - 那么请使用First()FirstOrDefault()

Dim Data = (From p In Db.TT_GENERAL_CONFIGURATION _
           Select p)
           .FirstOrDefault();

您的Data现在只包含结果的第一个元素。如果没有找到Data则没有。

<强>更新

如果您希望函数返回列表,则必须创建列表,并将其返回:

Private Function ConvertGeneralConfig(ByVal aConfig As TT_GENERAL_CONFIGURATION) As List(Of Config)

        Dim pConfig As New Config
        pConfig.ConfigID = aConfig.INTERNAL_NUM
        pConfig.ConfigType = aConfig.CONFIG_TYPE
        pConfig.ConfigName = aConfig.CONFIG_NAME
        pConfig.ConfigValue = aConfig.CONFIG_VALUE

        Dim lst As New List(Of Config) 'Creates a list
        lst.Add(pConfig) ' add the object to the list
        Return lst  ' returns the list


    End Function

答案 1 :(得分:0)

好像你可能想要使用

If Data IsNot Nothing Then
    Return ConvertGeneralConfig(Data)
Else
    Return Nothing
End If