I'm looking for the easiest and fastest way to get DataRows from a DataTable. These DataRows have several columns including one with Integer and I only want the rows with the highest value.
Currently i get this result like this :
Dim maxValue = 0
For Each row In mDataTable.Rows
If row.valueCell > maxValue Then
maxValue = row.valueCell
End If
Next
Dim mDataTableBis = mDataTable.Clone
For Each row In mDataTable.Select("value = " & valueCell)
mDataTableBis.ImportRow(row)
Next
Original DataTable (for example):
| Rows | letters | value |
|---|---|---|
| row 1 (wanted) | x | 4 |
| row 2 | y | 2 |
| row 3 (wanted) | z | 4 |
If you prefer the for-each loop approach, like it seems from the code you posted, use this function:
If you would like something more concise, you can try with LINQ but this is a more advanced solution, surely not beginner-friendly.
Something like:
You could also use the
DataTable.Select()method......but this is significally slower than a for each loop:
As you can see, the for each version is around 3500 times faster than the
Select()version - that's a big difference! This test has been executed on a relatively small DataTable with around 10k rows - imagine how big the difference could be in a million-rows-DataTable.