IT researcher
IT researcher

Reputation: 3304

How to use a 'Like' condition with wildcards on a DataTable

When I try to use the following Like condition on a DataTable I get an Exception:

For Each ResultRow As DataRow In tempDS.Tables(TableName).Select("Item like '<Root><Row>%<Rowid>%</Rowid></Row><Maxrowid>%</Maxrowid></Root> '")
    Resultval = Resultval & ResultRow.Item(Colname)
Next

How to match such a pattern?

Also i saw some articles stating there must not be wild character in the middle in case of datatable. So how the above case can be done?

Upvotes: 3

Views: 6831

Answers (1)

sloth
sloth

Reputation: 101072

Indeed, a wildcard in such expressions is only allowed at the start and end of a pattern, or at the end of a pattern, or at the start of a pattern.

But you can make use of VB.Net's Like Operator which allows such wildcards and filter the rows with e.g. a linq query.

Example:

Dim table = New DataTable()
table.Columns.Add("Item", GetType(String))

table.Rows.Add("Foo")
table.Rows.Add("Bar")
table.Rows.Add("FooBar")

Dim filtered = From row in table.AsEnumerable()
               Where row.Field(Of String)("Item") LIKE "F*o*"

For Each row In filtered
    Console.WriteLine(row("Item"))
Next

Output:

Foo
FooBar

Upvotes: 2

Related Questions