一千萬個為什麽

搜索

VB.Net Linq數據表存在

我想使用Linq而不是以下功能:

Friend Function IsCollectionInTable2(ByVal apps As DataTable, ByVal collectionId As String) As Boolean
    For Each row As DataRow In apps.Rows
        If row("CollectionId").ToString = collectionId Then Return True
    Next
    Return False
End Function

我能做的最好的事情如下:

Friend Function IsCollectionInTable(ByVal apps As DataTable, ByVal collectionId As String) As Boolean
    Return (From row In apps.AsEnumerable()
             Where (row.Field(Of String)("CollectionId") = collectionId)
             Select row.Field(Of String)("CollectionId")).Count > 0
End Function

我想在上面的功能中使用Exists或Any。表現可能是一個問題,

最佳答案

我找到了一個似乎有效的解決方案:

Return (From row In apps.AsEnumerable()
        Where row.Field(Of String)("CollectionId") = collectionId).Any()

我希望這和以下一樣快:

For Each row As DataRow In apps.Rows
If row("CollectionId").ToString = collectionId Then Return True
Next
Return False

轉載註明原文: VB.Net Linq數據表存在