0

I want to read a decimal value and make a decision using it. If value is greater than 0.75 then store a case else don't. I am dividing 4/5 and the answer is 0.8 but the variable is storing 0 instead of 0.8.

Code of the class is given below

    float value;

    List<crime> finalList = new List<crime>();
    Dictionary<int,float> finalDictionary = new Dictionary<int,float>(); 


    public float matching()
    {
        string crimeType = null;
        string keys = null;
        List<crime> keywordsFromOldCrimes = new List<crime>();
        List<String> keywordsFromCrimes = new List<string>();

        List<string> commonWords = new List<string>();
        SqlConnection conn = new SqlConnection(@"Data Source=DELL\SQLEXPRESS;initial catalog=CDMIS;integrated security=True;MultipleActiveResultSets=true;");
        SqlCommand commd = new SqlCommand("select * from crime where id=0 ", conn);
        SqlCommand commd1 = new SqlCommand("select * from crime", conn);
        SqlCommand commd2 = new SqlCommand("select crime.id , crime.crimeType, crime.keywords from crime inner join FIR on crime.FIRid= FIR.id where FIR.incidentDate in (select from FIR where datepart(yy, incidentDate)>2015)", conn);
        SqlCommand com3 = new SqlCommand("select * from crime where crimedate >= '2015-01-01'",conn);
        SqlDataReader read1;
        SqlDataReader read2;
        try
        {
            conn.Open();
            read1 = commd.ExecuteReader();
            while (read1.Read())
            {
                crimeType = read1.GetString(read1.GetOrdinal("crimeType"));
                keys = read1.GetString(read1.GetOrdinal("keywords"));
            }
        }
        catch (Exception exp)
        {
            throw exp;
        }

        read1.Close();
        List<string> keywords = keys.Split(',').ToList();

       // read2 = commd2.ExecuteReader();
        //while (read2.Read())
        //{
        //    keywords.Add(new crime()
        //    {
        //        id = read2.GetInt32(read2.GetOrdinal("id")),
        //        keywords = read2.GetString(read2.GetOrdinal("keywords")),
        //        crimeType = read2.GetString(read2.GetOrdinal("crimeType"))
        //    });
        //}
        //read2.Close();
        //foreach (var word in keyword)
        //{
        //    if (word.keywords == keys)
        //    {

        //    }
        //}
        read1 = com3.ExecuteReader();
        while (read1.Read())
        {
            keywordsFromOldCrimes.Add(new crime()
            {
                id = read1.GetInt32(read1.GetOrdinal("id")),
                caseStatus = read1.GetString(read1.GetOrdinal("caseStatus")),
                crimeType = read1.GetString(read1.GetOrdinal("crimeType")),
                details = read1.GetString(read1.GetOrdinal("details")),
                keywords = read1.GetString(read1.GetOrdinal("keywords")),

            });
        }

        foreach (var crime in keywordsFromOldCrimes)
        {
             keywordsFromCrimes = crime.keywords.Split(',').ToList();
             commonWords = keywords.Intersect(keywordsFromCrimes).ToList();
             value = commonWords.Count() / keywords.Count();
             Console.Write(value);
             if (value > 0.75)
             {
                 finalDictionary.Add(crime.id, value);
             }
             else
             {
                 Console.Write("nothing common");
             }
        }

        return (value);
    }

}

Problem lies in the following part:

    foreach (var crime in keywordsFromOldCrimes)
    {
         keywordsFromCrimes = crime.keywords.Split(',').ToList();
         commonWords = keywords.Intersect(keywordsFromCrimes).ToList();
         value = commonWords.Count() / keywords.Count();
         Console.Write(value);
         if (value > 0.75)
         {
             finalDictionary.Add(crime.id, value);
         }
         else
         {
             Console.Write("nothing common");
         }
    }

According to data in my database value of commonWords.count()=4 and value of keywords.count()=5 so answer in variable "value" should be "0.8" but it is "0".

1 Answer 1

1

This part

commonWords.Count() / keywords.Count()

is integer division, it needs to be casted to float for it to work properly

commonWords.Count() / (float) keywords.Count()
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.