// Method for use with load current when loading a community area search
        private void PerformSearchActionForCommArea(Database.CommunityArea CommArea)
        {
            IEnumerable<Database.CrimeType> crimes =
                    NIKernel.Instance.DBInstance.GetCommunityCrimeDataFromDataTable(Constants.DataTables.CrimesDataTable, CommArea.CommunityID);
            IEnumerable<int?> perCapitaIncome =
                NIKernel.Instance.DBInstance.GetPerCapitaIncome(Constants.DataTables.CensusDataTable, CommArea.CommunityID);
            IEnumerable<int?> hardshipIndex =
                NIKernel.Instance.DBInstance.GetHardshipIndex(Constants.DataTables.CensusDataTable, CommArea.CommunityID);

            Views.Controls.SearchDataResult sdr = new Controls.SearchDataResult(CommArea.CommunityName, crimes, perCapitaIncome, hardshipIndex);

            this.resultsListBox.Items.Clear();
            this.resultsListBox.BeginInit();
            this.resultsListBox.Items.Add(sdr.GetDataControl());
            this.resultsListBox.EndInit();
        }
        private void PerformSearchAction()
        {
            // Do these actions when searching by community...
            if (this.communityRadio.IsChecked == true)
            {
                IEnumerable<Database.CrimeType> crimes =
                    NIKernel.Instance.DBInstance.GetCommunityCrimeDataFromDataTable(Constants.DataTables.CrimesDataTable, ((Database.CommunityArea)this.searchSelectionComboBox.SelectedItem).CommunityID);
                IEnumerable<int?> perCapitaIncome =
                    NIKernel.Instance.DBInstance.GetPerCapitaIncome(Constants.DataTables.CensusDataTable, ((Database.CommunityArea)this.searchSelectionComboBox.SelectedItem).CommunityID);
                IEnumerable<int?> hardshipIndex =
                    NIKernel.Instance.DBInstance.GetHardshipIndex(Constants.DataTables.CensusDataTable, ((Database.CommunityArea)this.searchSelectionComboBox.SelectedItem).CommunityID);

                Views.Controls.SearchDataResult sdr = new Controls.SearchDataResult(
                    ((Database.CommunityArea)this.searchSelectionComboBox.SelectedItem).CommunityName, crimes, perCapitaIncome, hardshipIndex);

                this.resultsListBox.Items.Clear();
                this.resultsListBox.BeginInit();
                this.resultsListBox.Items.Add(sdr.GetDataControl());
                this.resultsListBox.EndInit();
            }
            // Do these actions when searching by block...
            if (this.blockRadio.IsChecked == true)
            {
                IEnumerable<decimal?> tif = 
                    NIKernel.Instance.DBInstance.GetTIFBalance(Constants.DataTables.TIFDataTable, this.searchSelectionComboBox.Text);

                Views.Controls.ByBlockSearchDataResult dr = new Controls.ByBlockSearchDataResult(
                    (string)this.searchSelectionComboBox.SelectedItem, tif.First());

                this.resultsListBox.Items.Clear();
                this.resultsListBox.BeginInit();
                this.resultsListBox.Items.Add(dr.GetDataControl());
                this.resultsListBox.EndInit();
            }
            // Do these actions when searching by statistic
            if (this.byStatRadio.IsChecked == true)
            {
                // Get a list of all the community names.
                EnumerableRowCollection<Database.CommunityArea> commAreasList = NIKernel.Instance.DBInstance.GetCommunityAreasFromDataTable(Constants.DataTables.CensusDataTable);
                // For each community area in the list, we need to get it's crime percentage, save all results in a list
                Dictionary<string, float> values = new Dictionary<string, float>();
                Dictionary<string, int?> valuesPCI = new Dictionary<string, int?>();
                foreach (Database.CommunityArea ca in commAreasList)
                {
                    if (this.refineSearchComboBox.Text == "Crime Rate")
                    {
                        float p = NIKernel.Instance.DBInstance.GetCommunityCrimeDataPercentage(Constants.DataTables.CrimesDataTable, ca.CommunityID);
                        values.Add(ca.CommunityName, p);
                    }
                    else if (this.refineSearchComboBox.Text == "Per Capita Income")
                    {
                        if (ca.CommunityID == 0) { continue; }
                        IEnumerable<int?> p = NIKernel.Instance.DBInstance.GetPerCapitaIncome(Constants.DataTables.CensusDataTable, ca.CommunityID);
                        valuesPCI.Add(ca.CommunityName, p.First());
                    }
                    else if (this.refineSearchComboBox.Text == "Hardship Index")
                    {
                        if (ca.CommunityID == 0) { continue; }
                        IEnumerable<int?> p = NIKernel.Instance.DBInstance.GetHardshipIndex(Constants.DataTables.CensusDataTable, ca.CommunityID);
                        valuesPCI.Add(ca.CommunityName, p.First());
                    }
                }
                // Now traverse the list of data and display the controls in the results list box
                if (this.refineSearchComboBox.Text == "Crime Rate")
                {
                    var sortedValuesAsc = from entry in values orderby entry.Value ascending select entry;
                    var sortedValuesDes = from entry in values orderby entry.Value descending select entry;
                    // Now we need to determine how many to display
                    int howMany = Convert.ToInt32(this.showTopTextBox.Text.Trim());
                    int index = 0;
                    this.resultsListBox.Items.Clear();
                    this.resultsListBox.BeginInit();
                    if (this.ascending.IsChecked == true)
                    {
                        foreach (KeyValuePair<string, float> kvp in sortedValuesAsc)
                        {
                            Console.WriteLine(kvp.Key + ": " + kvp.Value.ToString() + "%");

                            Views.Controls.StatSearchResult ssr = new Controls.StatSearchResult(kvp.Key, "Crime Rate", kvp.Value.ToString() + "% of all crimes in Chicago");
                            this.resultsListBox.Items.Add(ssr.GetDataControl());

                            if (index >= howMany)
                            {
                                break;
                            }
                            index++;
                        }
                    }
                    else
                    {
                        foreach (KeyValuePair<string, float> kvp in sortedValuesDes)
                        {
                            Console.WriteLine(kvp.Key + ": " + kvp.Value.ToString() + "%");

                            Views.Controls.StatSearchResult ssr = new Controls.StatSearchResult(kvp.Key, "Crime Rate", kvp.Value.ToString() + "% of all crimes in Chicago");
                            this.resultsListBox.Items.Add(ssr.GetDataControl());

                            if (index >= howMany)
                            {
                                break;
                            }
                            index++;
                        }
                    }
                    this.resultsListBox.EndInit();
                }
                else if (this.refineSearchComboBox.Text == "Per Capita Income")
                {
                    var sortedValuesAsc = from entry in valuesPCI orderby entry.Value ascending select entry;
                    var sortedValuesDes = from entry in valuesPCI orderby entry.Value descending select entry;
                    // Now we need to determine how many to display
                    int howMany = Convert.ToInt32(this.showTopTextBox.Text.Trim());
                    int index = 0;
                    this.resultsListBox.Items.Clear();
                    this.resultsListBox.BeginInit();
                    if (this.ascending.IsChecked == true)
                    {
                        foreach (KeyValuePair<string, int?> kvp in sortedValuesDes)
                        {
                            Console.WriteLine(kvp.Key + ": $" + kvp.Value.ToString() + ".00");

                            Views.Controls.StatSearchResult ssr = new Controls.StatSearchResult(kvp.Key, "Per Capita Income", kvp.Value.ToString() + ".00");
                            this.resultsListBox.Items.Add(ssr.GetDataControl());

                            if (index >= howMany)
                            {
                                break;
                            }
                            index++;
                        }
                    }
                    else
                    {
                        foreach (KeyValuePair<string, int?> kvp in sortedValuesAsc)
                        {
                            Console.WriteLine(kvp.Key + ": $" + kvp.Value.ToString() + ".00");

                            Views.Controls.StatSearchResult ssr = new Controls.StatSearchResult(kvp.Key, "Per Capita Income", kvp.Value.ToString() + ".00");
                            this.resultsListBox.Items.Add(ssr.GetDataControl());

                            if (index >= howMany)
                            {
                                break;
                            }
                            index++;
                        }
                    }
                    this.resultsListBox.EndInit();
                }
                else if (this.refineSearchComboBox.Text == "Hardship Index")
                {
                    var sortedValuesAsc = from entry in valuesPCI orderby entry.Value ascending select entry;
                    var sortedValuesDes = from entry in valuesPCI orderby entry.Value descending select entry;
                    // Now we need to determine how many to display
                    int howMany = Convert.ToInt32(this.showTopTextBox.Text.Trim());
                    int index = 0;
                    this.resultsListBox.Items.Clear();
                    this.resultsListBox.BeginInit();
                    if (this.ascending.IsChecked == true)
                    {
                        foreach (KeyValuePair<string, int?> kvp in sortedValuesAsc)
                        {
                            Console.WriteLine(kvp.Key + ": " + kvp.Value.ToString());

                            Views.Controls.StatSearchResult ssr = new Controls.StatSearchResult(kvp.Key, "Hardship Index", kvp.Value.ToString() + " out of 100");
                            this.resultsListBox.Items.Add(ssr.GetDataControl());

                            if (index >= howMany)
                            {
                                break;
                            }
                            index++;
                        }
                    }
                    else
                    {
                        foreach (KeyValuePair<string, int?> kvp in sortedValuesDes)
                        {
                            Console.WriteLine(kvp.Key + ": " + kvp.Value.ToString());

                            Views.Controls.StatSearchResult ssr = new Controls.StatSearchResult(kvp.Key, "Hardship Index", kvp.Value.ToString() + " out of 100");
                            this.resultsListBox.Items.Add(ssr.GetDataControl());

                            if (index >= howMany)
                            {
                                break;
                            }
                            index++;
                        }
                    }
                    this.resultsListBox.EndInit();
                }
                
            }
        }