我覺得這個功能太長了。我可以將它分爲兩個功能,但我寧願將它們放在一個這樣的功能中。您可以在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查詢本身。唯一需要改變的是列表的順序。或者,還有另一種方法來命令我返回的結果嗎?