Problem with DataTable.Select
Is there a way to find a value in DataTable use method Selec开发者_StackOverflow社区t:
items.Select("Number like '%10%'");
But Number type is Int, and this code doesn`t work...
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.
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.
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%'");
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;
If memory serves you need to escape a single quote by doubling it. Ie:
items.Select("Number like ''%10%''");
You can use DataView to filter values in DataTable
DataView dv = new DataView(dtSample);
dv.RowFilter = "Number like '%10%'";
精彩评论