DataTable.CaseSensitivity affect on relational selects

I am trying to modify how a DataTable.Select behaves with respect to case sensitivity. The documentation implies that:

The CaseSensitive property affects string comparisons in sorting, searching, and filtering.

Modifying the example to use a relative operater and not the equal operator I get the same results independent of the value of the CaseSensitive property.

  • Is this a bug
  • Is there a way to affect relational operators in a select statement

private static void ToggleCaseSensitive()

{

DataTable t;

DataRow[] foundRows;

t = CreateDataSet().Tables[0];

t.CaseSensitive = false;

foundRows = t.Select("item > 'aac'");

// Print out DataRow values.

PrintRowValues(foundRows, "CaseSensitive = False");

t.CaseSensitive = true;

foundRows = t.Select("item > 'aac'");

PrintRowValues(foundRows, "CaseSensitive = True");

}

public static DataSet CreateDataSet()

{

// Create a DataSet with one table, two columns

DataSet ds = new DataSet();

DataTable t = new DataTable("Items");

// Add table to dataset

ds.Tables.Add(t);

// Add two columns

DataColumn c;

// First column

c = t.Columns.Add("id", typeof(int));

c.AutoIncrement = true;

// Second column

t.Columns.Add("item", typeof(string));

// Set primary key

t.PrimaryKey = new DataColumn[] { t.Columns["id"] };

// Add twelve rows

for (int i = 0; i < 10; i++)

{

t.Rows.Add(new object[] { i, i.ToString() });

}

t.Rows.Add(new object[] { 10, "aac" });

t.Rows.Add(new object[] { 11, "abc" });

t.Rows.Add(new object[] { 12, "Abc" });

t.Rows.Add(new object[] { 13, "ABC" });

return ds;

}

private static void PrintRowValues(DataRow[] rows, string label)

{

Console.WriteLine();

Console.WriteLine(label);

if (rows.Length <= 0)

{

Console.WriteLine("no rows found");

return;

}

foreach (DataRow r in rows)

{

foreach (DataColumn c in r.Table.Columns)

{

Console.Write("\t {0}", rCoffee);

}

Console.WriteLine();

}

}



Answer this question

DataTable.CaseSensitivity affect on relational selects

  • jeff2

    Thank you. You are absolutely correct. DataTables, and SQL Server consider

    1. aac
    2. abc
    3. ABc
    4. ABC
    5. bbc
    6. BBC

    as the correct order. Unfortunately, I am working with a source that considers the following as the correct order.

    1. aac
    2. abc
    3. bbc
    4. ABc
    5. ABC
    6. BBC

    It uses a strict ASCII order where "b" is less than "A". I mastakenly assumed that DataTables behaved similarly.

    • Do you have any suggestions as to how to make a DataTable behave this way


  • GBez

    Unfortunately DataTable does not expose the options it uses to compare strings, so you cannot change them.

    The sort is culture sensitive, so if you change the culture in DataTable.Locale you may be able to change the ordering. That said, i actually think there's no culture where the default sorting is ASCII like, and i don't think you can create cultures.

    You can create your own type which implements IComparable, but then expression filters do not support the use of UDTs (user defined types) (they don't know how to go from string to UDT). Sort expressions support UDTs, so you can create a DataView ordered the way you want and then do some custom processing on top of it...

    Sorry,

    --VV [MS]


  • Russ2

    It is working correctly.

    Your query is asking for > 'aac'. lower capital a is lower than capital A.
    All other 3 rows are > 'aac' whether casesensitive is on or not. That's why you get them on both queries.

    Try doing > 'abc'. You'll get 0 or 3 rows back.

    --VV [MS]


  • DataTable.CaseSensitivity affect on relational selects