I am writing a program in C#.NET
I would like to gather the total frequency of a class (let imagine every class has many words and each word has its own frequency in the corresponding class)
So i used the sum() function in mysql. But there is an error saying that my cast is wrong.
public void average_each_type()
{
MySqlDataReader result;
int total_freq = 0;
string type = "";
command.CommandText = "select class_name ,SUM(frequency) as sum_of_freq from training_set group by class_name ";
result = command.ExecuteReader();
while (result.Read())
{
total_freq = (int)result["sum_of_freq"]; //error happened here
type = result["class_name"].ToString();
//.....then so on...//
SUM in MySql will return a decimal or double value, depending on the type within “frequency”. From the documentation for SUM:
If you want an integer, you can use
Convertto get one no matter what the source type happens to be:The advantage here is the Convert.ToInt32 will work no matter what type of value is returned from the database, provided it is a numeric type.