V
V
vxd_dev2014-06-15 00:31:48
.NET
vxd_dev, 2014-06-15 00:31:48

DataTable sort column of type String as number?

Hello everyone
, please help me sort the rows in the DataTable correctly by the column that stores the values ​​in the hexadecimal system, that is, the strings of the "0x0..F" format. Now I do this:

var dataRows = (from row in ds.Tables[0].Select()
                                orderby Int32.Parse(row["Type"].ToString().TrimStart(new[] { '0', 'x' }),
                                                    NumberStyles.HexNumber |
                                                    NumberStyles.AllowHexSpecifier) ascending
                                select r).AsEnumerable();

but somehow I can’t believe that everything is so complicated, maybe there is some more correct and faster way, even the manual enumeration algorithm will arrange it, if only it were fast.
Another problem of my decision - with incorrect data - immediately falls, because Int32.Parse does not like the string. In principle, there should not be incorrect data, but all the same, such behavior when sorting somehow seems wrong, and try blocks to fence - so these are generally some kind of crutches.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
G
gleb_kudr, 2014-06-15
@vxd_dev

With an error, everything is simple. Use Int32.TryParse
But it's better to use the special converter Convert.ToUInt32 right away . I think it would be better not to do this here.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question