Using Generics for DAL Filtering

I ran across a project recently when I needed to be able to filter a large number of table adapters across a project. I guess I could have written the a bunch of code in order to handle the filtering on each object individually but simplicity is often the best recipe. This was a perfect instance to use generics in order to totally simplify what I needed to do. The below function is used to pass in an type of data table and a specific filter to be applied and receive the same typed object back in its filtered state.

Hopefully, this helps some people out that have been reluctant to make the dive into generics and see how it can make some aspects of your programming life just that much easier.

Public Function FilterDataTable(Of T As {DataTable, New})(ByRef srcTable As T, ByVal filter As String) As T

Dim rtnTable As T = New T()

Dim rows() As DataRow = srcTable.Select(filter)

For Each row As DataRow In rows

rtnTable.ImportRow(row)

Next

Return rtnTable

End Function

Cheers,

AJ