Why does DataTable.Select() return the wrong rows?
Asked Answered
D

2

6

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);
Decimeter answered 31/3, 2011 at 18:1 Comment(1)
An interesting property of this problem is that if you rearrange the columns in the DataTable, the queries return different results. I tried moving the ID column from the first to the third column in the DataTable, and the first Select() returns no rows, as expected.Olive
R
4

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'")
Regelation answered 31/3, 2011 at 20:30 Comment(0)
A
0

Not an answer.

I modified the test to make it a little easier to work with and found more incorrect:

A AND B AND ID = '00000000-0000-0000-0000-000000000000'

B AND A AND (ID = '0000000-0000-0000-0000-000000000000')

This looks like a bug to me. You might want to take it to Microsoft Connnect.

using System;
using System.Data;
using System.Diagnostics;

class Program
{
    static void Main(string[] args)
    {
        DataTable dt = new DataTable("dt")
        {
            Columns =
            {
              new DataColumn("ID", typeof(Guid)),
              new DataColumn("A", typeof(bool)),
              new DataColumn("B", typeof(bool)),
            }
        };

        dt.Rows.Add(Guid.Empty, false, false);
        dt.Rows.Add(Guid.Empty, false, true);
        dt.Rows.Add(Guid.Empty, true, false);
        dt.Rows.Add(Guid.Empty, true, true);

        Console.BackgroundColor = ConsoleColor.Black;
        Console.Clear();
        Console.ForegroundColor = ConsoleColor.Green;
        foreach (DataRow row in dt.Rows)
            Console.WriteLine("ID = {0}, A = {1}, B = {2}", row["ID"], row["A"], row["B"]);
        Console.WriteLine();

        // this incorrectly returns a row
        Test(dt, "B AND A AND ID = '00000000-0000-0000-0000-000000000000'");

        // these correctly return 0 rows
        Test(dt, "B AND A");
        Test(dt, "B AND A AND CONVERT(ID, 'System.String') = '00000000-0000-0000-0000-000000000000'");
        Test(dt, "A AND B AND ID = '00000000-0000-0000-0000-000000000000'");
        Test(dt, "B = 1 AND A AND ID = '00000000-0000-0000-0000-000000000000'");
        Test(dt, "ID = '00000000-0000-0000-0000-000000000000' AND B AND A");
        Test(dt, "B AND (A AND ID = '00000000-0000-0000-0000-000000000000')");
        Test(dt, "(B AND A AND ID = '00000000-0000-0000-0000-000000000000')");

        // still wrong
        Test(dt, "B AND A AND ID = '00000000-0000-0000-0000-000000000000'");

        // also incorrect for both A = True and B = True
        Test(dt, "B AND A AND (ID = '0000000-0000-0000-0000-000000000000')");

        if (Debugger.IsAttached)
        {
            Console.ForegroundColor = ConsoleColor.Gray;
            Console.WriteLine();
            Console.WriteLine("Press any key to continue . . . ");
            Console.ReadKey();
        }

        Console.ResetColor();
        Console.Clear();
    }

    public static void Test(DataTable dt, string filter)
    {
        Console.ForegroundColor = ConsoleColor.White;
        Console.WriteLine(filter);
        Console.ForegroundColor = ConsoleColor.Yellow;
        Console.WriteLine("    DT = {0}, DV = {1}",
            dt.Select(filter).Length,
            new DataView { Table = dt, RowFilter = filter }.Count);
    }
}
Amaranthine answered 31/3, 2011 at 20:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.