I have a data table in my database and it has only one column which stores unique keys in string data type. The snippet code and its data as shown below
CREATE TABLE MyTable (
KEY_CODE varchar(5)
);
insert into MyTable(KEY_CODE) values('00');
insert into MyTable(KEY_CODE) values('-01');
insert into MyTable(KEY_CODE) values('01');
insert into MyTable(KEY_CODE) values('02');
insert into MyTable(KEY_CODE) values('03');
insert into MyTable(KEY_CODE) values('T');
Then I query all my KEY_CODE items and sort them by KEY_CODE ASC
select * from MyTable
order by KEY_CODE
And here is the output result in MS SQL Server Management Studio. You can see that MS SSMS understand the key "-01" is smaller than "00" and "01".
Now, in my VB.NET project, I also have a data table with the same column and data. But the order of the KEY_CODE column is not matched with the one from MS SSMS. You can see the output data table in Debug mode that I mention in the captured image below. The "-01" key is not at the top of the result.
I am expecting the order result from LINQ to SQL should be the same as the one from MS SSMS. How can I achieve this?
Dim myTable As DataTable = New DataTable("MyTable")
Dim col As DataColumn = New DataColumn("KEY_CODE")
col.DataType = System.Type.GetType("System.String")
myTable.Columns.Add(col)
Dim row1 As DataRow = myTable.NewRow()
Dim row2 As DataRow = myTable.NewRow()
Dim row3 As DataRow = myTable.NewRow()
Dim row4 As DataRow = myTable.NewRow()
Dim row5 As DataRow = myTable.NewRow()
Dim row6 As DataRow = myTable.NewRow()
row1.Item("KEY_CODE") = "00"
row2.Item("KEY_CODE") = "-01"
row3.Item("KEY_CODE") = "01"
row4.Item("KEY_CODE") = "02"
row5.Item("KEY_CODE") = "03"
row6.Item("KEY_CODE") = "T"
myTable.Rows.Add(row1)
myTable.Rows.Add(row2)
myTable.Rows.Add(row3)
myTable.Rows.Add(row4)
myTable.Rows.Add(row5)
myTable.Rows.Add(row6)
Dim datarows As DataRow() = myTable.Select()
Dim output = (From row In datarows
Order By "KEY_CODE ASC"
Select row Distinct).CopyToDataTable()

