2010-05-05 3 views
0

Je voudrais utiliser LINQ au lieu de la fonction ci-dessous:VB.Net Linq Datatable Exists

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 

Le meilleur que je peux faire est ci-dessous:

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 

Je voudrais utiliser ou Existant Tout dans la fonction ci-dessus. Performance pourrait être un problème,

Répondre

0

Qu'en est-DataTable.Select Method (String) (désolé mon VB est pauvre)

Dim expression As String 
    expression = string.Format("CollectionId = '{0}'", collectionId) 
    ' Use the Select method to find all rows matching the filter. 
    return apps.Select(expression).Count > 0 
+0

Mieux que mon exemple, mais il doit encore lire toutes les lignes. Je voudrais utiliser Exists ou Any pour qu'il puisse revenir le plus tôt possible. – LarsH

1

J'ai trouvé une solution qui semble fonctionner:

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

J'espère que cela est aussi rapide que :

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