locked
A Strange question about DataTable.Seletc()! RRS feed

  • Question

  • I have a strange question about DataTable.Seletc()
    Please refer the source below:

            private void Form2_Load(object sender, EventArgs e)
            {
                DataTable dat = new DataTable();
                dat.Columns.Add(new DataColumn("String", typeof(string)));
                dat.Columns.Add(new DataColumn("Int32", typeof(int)));

                AddDataToTable(dat, 3, "616", 616);
                AddDataToTable(dat, 3, "937", 937);
                AddDataToTable(dat, 3, "1229", 1229);
                AddDataToTable(dat, 3, "856", 856);
                AddDataToTable(dat, 3, "944", 944);
                AddDataToTable(dat, 3, "604", 604);
                AddDataToTable(dat, 3, "804", 804);
                AddDataToTable(dat, 3, "769", 769);
                AddDataToTable(dat, 3, "681", 681);
                AddDataToTable(dat, 3, "1", 1);
                string filter = "String = 937";
                DataRow[] rows = dat.Select(filter);

            }
           
            private void AddDataToTable(DataTable table, int count, params object[] values)
            {
                for (int i = 0; i < count; i++)
                {
                    DataRow row = table.NewRow();
                    row.ItemArray = values;
                    table.Rows.Add(row);
                }
                table.AcceptChanges();
            }

    I know I can fetch the correct result with filter like "String = '1229'" or "Int32 = 1229".     
    But when I use the incorrect filter like "String = 1229" or "String = 616",
    sometime the result is correct, sometime is not.
    Why? Who knows the reason of this issue?

    Wednesday, May 11, 2011 10:27 AM

Answers

  • Hey Dongjh,

    While writing the criteria to filter the rows in DataTable.Select Method use the SQL syntax, e.g., in your case you better write the filterExpression as String Like '1229' rather than String = '1229' or String = 1229, Select should filter your criteria exactly.

    -Mohammed Ghouse Ibne Barq Kadapavi
    http://www.ProgrammersVision.blogspot.com
    https://sites.google.com/site/BarqKadapavi
    Please mark the post answered your question as the answer, and mark other helpful posts as helpful.


    Mohammed Ghouse Ibne Barq Kadapavi
    • Marked as answer by Jackie-Sun Tuesday, May 31, 2011 2:51 AM
    Thursday, May 12, 2011 9:37 AM

All replies

  • I think what's going on here is that Select parses 937 as a number and converts it to a string internally. Thus it works with integer numbers.
    Miha Markic [MVP C#] http://blog.rthand.com
    Thursday, May 12, 2011 9:02 AM
  • Hey Dongjh,

    While writing the criteria to filter the rows in DataTable.Select Method use the SQL syntax, e.g., in your case you better write the filterExpression as String Like '1229' rather than String = '1229' or String = 1229, Select should filter your criteria exactly.

    -Mohammed Ghouse Ibne Barq Kadapavi
    http://www.ProgrammersVision.blogspot.com
    https://sites.google.com/site/BarqKadapavi
    Please mark the post answered your question as the answer, and mark other helpful posts as helpful.


    Mohammed Ghouse Ibne Barq Kadapavi
    • Marked as answer by Jackie-Sun Tuesday, May 31, 2011 2:51 AM
    Thursday, May 12, 2011 9:37 AM