Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/15.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

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(XML)查询中添加If语句?_Vb.net_Linq - Fatal编程技术网

Vb.net 能否在LINQ(XML)查询中添加If语句?

Vb.net 能否在LINQ(XML)查询中添加If语句?,vb.net,linq,Vb.net,Linq,我觉得这个函数太长了。我可以将它分为两个函数,但我更愿意将它们保留在一个函数中 Public Function getTempList(ByVal applicationType As String) As List(Of String) Dim doc As XDocument = New XDocument If My.Settings.sortKey = "alpha" Then Dim XMLquery = From c In doc.<appl

我觉得这个函数太长了。我可以将它分为两个函数,但我更愿意将它们保留在一个函数中

Public Function getTempList(ByVal applicationType As String) As List(Of String)
    Dim doc As XDocument = New XDocument


    If My.Settings.sortKey = "alpha" Then
        Dim XMLquery = From c In doc.<applications>.<app> _
           Where c.<appFav>.Value = "true" And c.<appType>.Value = applicationType
           Order By CStr(c.<appName>.Value)
           Select c.<appName>
        Dim tempList As New List(Of String)
        For Each result In XMLquery
            tempList.Add(result.Value)
        Next
        Return tempList
    ElseIf My.Settings.sortKey = "fav" Then ' ------------------------------------------------------------------------------------ 
        Dim XMLquery = From c In doc.<applications>.<app> _
           Where c.<appFav>.Value = "true" And c.<appType>.Value = applicationType
           Order By CInt(c.<appClick>.Value) Descending
           Select c.<appName>
        Dim tempList As New List(Of String)
        For Each result In XMLquery
            tempList.Add(result.Value)
        Next
        Return tempList
    End If

End Function

我可以把if语句放在LINQ查询本身中吗。这里唯一需要更改的是列表的顺序。或者,是否有其他方法对我返回的结果进行排序?

您不需要重复整个查询

您可以执行以下操作:

请原谅我的VB语法,我通常用C编写代码

Public Function getTempList(ByVal applicationType As String) As List(Of String)
    Dim doc As XDocument = New XDocument
    Dim tempList As New List(Of String)

    Dim XMLquery = From c In doc.<applications>.<app> _
       Where c.<appFav>.Value = "true" And c.<appType>.Value = applicationType
       Select c.<appName>

    If My.Settings.sortKey = "aplha" Then
        Order XMLQuery By CStr(c.<appName>.Value)   // convert to VB code

    If My.Settings.sortKey = "fav" Then
        Order XMLQuery By CInt(c.<appClick>.Value) Descending //convert to VB code

    For Each result In XMLquery
        tempList.Add(result.Value)
    Next

    Return tempList
End Function
试试这个

 Public Function getTempList(ByVal applicationType As String) As List(Of String)
        Dim doc As XDocument = New XDocument
        Dim XMLquery = From c In doc.<applications>.<app> _
              Where c.<appFav>.Value = "true" And c.<appType>.Value = applicationType
              Select c
        Dim tempList As New List(Of String)

        If My.Settings.sortKey = "alpha" Then

            XMLquery = XMLquery.OrderBy(Function(c) CStr(c.<appName>.Value))

        ElseIf My.Settings.sortKey = "fav" Then

            XMLquery = XMLquery.OrderByDescending(Function(c) CInt(c.<appClick>.Value))

        End If

        For Each result In XMLquery
            tempList.Add(result.<appName>.Value)
        Next
        Return tempList
    End Function

我认为这是最简单的:

Public Function getTempList(ByVal applicationType As String) As List(Of String)

    Dim doc As XDocument = New XDocument

    Dim XMLquery = _
        From c In doc.<applications>.<app> _
        Where c.<appFav>.Value = "true" And c.<appType>.Value = applicationType _
        Select c

    If My.Settings.sortKey = "alpha" Then
        XMLquery = XMLquery.OrderBy(Function(c) CStr(c.<appName>.Value))
    ElseIf My.Settings.sortKey = "fav" Then
        XMLquery = XMLquery.OrderByDescending(Function(c) CInt(c.<appClick>.Value))
    End If

    Return XMLquery.Select(Function(x) x.<appName>.Value).ToList()

End Function