DataTable.Select string / int comparison

I'm crazy because my grid is not showing correctly and found that I am comparing varchar columns (only numeric values) without using '(quote). The problem is that for some numbers the coincidence of the choice and for the other choice do not match.

This is an example:

DataTable tab = new DataTable(); tab.Columns.Add("age", typeof(String)); DataRow row1 = tab.NewRow(); row1["age"] = "8"; tab.Rows.Add(row1); DataRow row2 = tab.NewRow(); row2["age"] = "15"; tab.Rows.Add(row2); Console.WriteLine("Rows with age 8="+ tab.Select("age=8").Length); Console.WriteLine("Rows with age 15=" + tab.Select("age=15").Length); 

Exit:

 Rows with age 8=0 Rows with age 15=1 

Why for 8 numbers do not match and for 15 numbers yes? This is mistake?

+4
source share
1 answer

This seems to be a bug (function?) In the internal string matching / int. You must either add single quotes to compare data as strings, or if you need to compare them as integers, and you can use LINQ, you can do something like

 int i = tab.AsEnumerable().Where(x => Convert.ToInt32(x["age"]) == 8).Count(); // 1 
0
source

Source: https://habr.com/ru/post/1499833/


All Articles