I have a DataTable with data in a string column.
Values:
“1”
“{”
“2”
When I select data from the DataTable sorting by the column I would like to get the values as:
“1”
“2”
“{“
but instead, I get the values as:
“{”
“1”
“2”
If I needed to do it in sql server I would use the collation “Latin1_general_bin”.
I need that same behavior in DataTable.
Here is my test:
[Test]
public void TestDataTable()
{
var dt = new DataTable();
dt.Columns.Add("a", typeof(string));
dt.Rows.Add("1");
dt.Rows.Add("{");
dt.Rows.Add("2");
int i = 0;
foreach (var val in dt.Select("", "a"))
{
Assert.AreEqual(new string[] {"1", "2", "{"}[i++],val["a"]);
}
}
The best solution I can come up with at this time is, setting the private field _compareFlags using reflection.
I know, I hate reflection for this, and it might stop working at some unknown time, but I have a lot of unit tests for it so I hope I will not live to regret this.
Any better solution is welcome, I thought of creating my own culture info, but couldn’t see it through (open to suggestions on this path).
Here is what I did with reflection: