views:

176

answers:

2

I would like to use Linq instead of below function :

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

The best I can do is below:

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

I would like to use Exists or Any in above function. Performance could be an issue,

A: 

What about DataTable.Select Method (String) (sorry my VB is poor)

    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
Preet Sangha
Better than my example but still it have to read all rows. I would like to use Exists or Any so that it can return as soon as possible.
LarsH
A: 

I found a solution that seems to work :

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

I hope this is as fast as :

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