The DataTable.Select() function returns the wrong rows with a filter like this...
"booleanColumn1 AND booleanColumn2 AND GuidColumn1 = '00000000-0000-0000-0000-000000000000')"
Making virtually any alteration to this format fixes it (see example). Using the same filter on a dataView works correctly. I'm tempted to change it to
"booleanColumn1 = 1 AND booleanColumn2 = 1 AND GuidColumn1 = '00000000-0000-0000-0000-000000000000')"
and declare it fixed (the documentation makes no mention of whether "A" or "A = 1" is the correct syntax for boolean columns). But the blame could just as easily be placed on the Guid column. Before I revisit the hundreds of places we use DataTable.Select() in our codebase, I was hoping to see if anyone knew what was really going on.
DataTable dt = new DataTable("dt");
dt.Columns.AddRange(new DataColumn[]
{
new DataColumn("ID", typeof(Guid)),
new DataColumn("A", typeof(bool)),
new DataColumn("B", typeof(bool))
});
dt.Rows.Add(Guid.Empty, false, true);
// this incorrectly returns a row
Debug.WriteLine(dt.Select("B AND A AND ID = '00000000-0000-0000-0000-000000000000'").Length);
// yet it's fine for a DataView (correctly returns 0 rows)
DataView dv = new DataView(dt);
dv.RowFilter = "B AND A AND ID = '00000000-0000-0000-0000-000000000000'";
Debug.WriteLine(dv.Count);
// these correctly return 0 rows
Debug.WriteLine(dt.Select("B AND A").Length);
Debug.WriteLine(dt.Select("B AND A AND CONVERT(ID, 'System.String') = '00000000-0000-0000-0000-000000000000'").Length);
Debug.WriteLine(dt.Select("A AND B AND ID = '00000000-0000-0000-0000-000000000000'").Length);
Debug.WriteLine(dt.Select("B = 1 AND A AND ID = '00000000-0000-0000-0000-000000000000'").Length);
Debug.WriteLine(dt.Select("ID = '00000000-0000-0000-0000-000000000000' AND B AND A").Length);
Debug.WriteLine(dt.Select("B AND (A AND ID = '00000000-0000-0000-0000-000000000000')").Length);
// still wrong
Debug.WriteLine(dt.Select("B AND A AND ID = '00000000-0000-0000-0000-000000000000'").Length);
This is definitely a bug, and it looks like it has been around for a long time. I found this knowledgebase article that describes the exact same bug in .Net framework 1.1.
It seems like the second condition is completely ignored, for I found that the following variations also return one row:
dt.Select("B AND false AND ID = '00000000-0000-0000-0000-000000000000'")
dt.Select("B AND 0 AND ID = '00000000-0000-0000-0000-000000000000'")
This however correctly returns 0 rows:
dt.Select("B AND A AND A AND ID = '00000000-0000-0000-0000-000000000000'")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With