2013-12-22 42 views
1

我覺得這個功能太長了。我可以將它分爲兩​​個功能,但我寧願將它們放在一個這樣的功能中。您可以在LINQ(XML)查詢中添加If語句嗎?

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查詢本身。唯一需要改變的是列表的順序。或者,還有另一種方法來命令我返回的結果嗎?

回答

1

我認爲這將是最簡單的:

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 
0

你不需要重複整個查詢。

你可以這樣做:

原諒我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 
1

試試這個,

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 
相關問題