Roman N
Roman N

Reputation: 5126

Problem with DataTable.Select

Is there a way to find a value in DataTable use method Select: items.Select("Number like '%10%'");

But Number type is Int, and this code doesn`t work...

Upvotes: 1

Views: 6375

Answers (6)

BenKoshy
BenKoshy

Reputation: 35725

For those reading in the future, this might help:

DataRow[] matchingRows = a.Select("Convert(Age, 'System.String') like '2%'   "); // cut and paste from my code

So in your code it would be:

DataRow[] matchingRows = a.Select("Convert(Number, 'System.String') like '10%' "); // where Number is the name of your column.

Upvotes: 1

Jason Evans
Jason Evans

Reputation: 29186

Look at the T-SQL CAST and CONVERT functions, they might help you here. Basically, in your query, use either CAST or CONVERT (not sure which is best suited for you) to change the int into a string that you can perform a LIKE on.

Upvotes: 1

Sukhjeevan
Sukhjeevan

Reputation: 3156

You can use DataView to filter values in DataTable

DataView dv = new DataView(dtSample);
dv.RowFilter = "Number like '%10%'";

Upvotes: 0

Phil Gan
Phil Gan

Reputation: 2863

If memory serves you need to escape a single quote by doubling it. Ie:

items.Select("Number like ''%10%''");

Upvotes: 0

vc 74
vc 74

Reputation: 38179

You can use linq the following way:

var matchingRows =
    from item in items.AsEnumerable()
    where item.Field<string>("Number").Contains("10")
    select item;

or if Number is actually a number:

var matchingRows =
    from item in items.AsEnumerable()
    where item.Field<double>("Number").ToString().Contains("10")
    select item;

Upvotes: 0

apros
apros

Reputation: 2878

If you really want to have such behaviour, then it would be better just add to your table additional computed column like "NumberText" with type String, which will store the same values as for column Number. Then you can execute your expression: items.Select("NumberText like '%10%'");

Upvotes: 0

Related Questions