private void avgStarsPerCategoryButton_Click(object sender, RoutedEventArgs e) { Dictionary <string, int> categoryCount = new Dictionary <string, int>(); Dictionary <string, int> categoryStarSum = new Dictionary <string, int>(); List <string> cat = new List <string>(); for (int i = 0; i < searchResultsDataGrid.Items.Count; i++) { Business b = (Business)searchResultsDataGrid.Items[i]; cat.Clear(); using (var connection = new NpgsqlConnection(buildConnString())) { connection.Open(); using (var cmd = new NpgsqlCommand()) { cmd.Connection = connection; cmd.CommandText = "SELECT category FROM Categories WHERE business_id='" + b.id + "' "; using (var reader = cmd.ExecuteReader()) { //gets all categories for business into list while (reader.Read()) { cat.Add(reader.GetString(0)); } } foreach (string s in cat) { cmd.CommandText = "SELECT stars FROM business WHERE business_id='" + b.id + "' ";//adds stars for each category to a list for count and sum FIND AVG (sum/count) using (var reader = cmd.ExecuteReader()) { while (reader.Read()) { if (categoryCount.ContainsKey(s)) { categoryCount[s] += 1; categoryStarSum[s] += reader.GetInt32(0); } } } } } } } CategoriesChart w2 = new CategoriesChart(); foreach (KeyValuePair <string, int> kvp in categoryStarSum) { categoryStarSum[kvp.Key] = kvp.Value / categoryCount[kvp.Key]; } w2.categoryChart.DataContext = categoryStarSum; w2.Show(); }
private void numBusinessPerCategoryButton_Click(object sender, RoutedEventArgs e) { Dictionary <string, int> myData = new Dictionary <string, int>(); for (int i = 0; i < searchResultsDataGrid.Items.Count; i++) { Business b = (Business)searchResultsDataGrid.Items[i]; using (var connection = new NpgsqlConnection(buildConnString())) { connection.Open(); using (var cmd = new NpgsqlCommand()) { cmd.Connection = connection; cmd.CommandText = "SELECT category FROM Categories WHERE business_id='" + b.id + "' "; using (var reader = cmd.ExecuteReader()) { while (reader.Read()) { if (myData.ContainsKey(reader.GetString(0))) { myData[reader.GetString(0)] += 1; } else { myData[reader.GetString(0)] = 1; } } } } } } CategoriesChart w2 = new CategoriesChart(); w2.categoryChart.DataContext = myData; w2.Show(); }