Exemple #1
0
        private async void GetSelectedButton_Click(object sender, EventArgs e)
        {
            ResultsTextBox.Text = "";
            var sb = new StringBuilder();

            /*
             * Get all checked categories primary key in the checked list box
             */
            var indices = CategoryCheckedListBox.SelectedCategoryIdentifier();

            if (indices.Length <= 0)
            {
                return;
            }

            using (var context = new NorthWindContext())
            {
                /*
                 * Get all products for each category, not FindAllAsync expects an
                 * object array not an int array so they are converted via
                 * Array.ConvertAll.
                 */
                Categories[] categories = await context.FindAllAsync <Categories>(Array.ConvertAll(indices, id => (object)id));

                /*
                 * Display in a text box
                 */
                foreach (Categories category in categories)
                {
                    sb.AppendLine(category.CategoryName);

                    List <Products> products = context.Products
                                               .AsNoTracking().Where(prod => prod.CategoryId == category.CategoryId)
                                               .ToList();

                    foreach (Products product in products)
                    {
                        sb.AppendLine($"  {product.ProductId,-4}{product.ProductName}");
                    }

                    sb.AppendLine();
                }

                ResultsTextBox.Text = sb.ToString();
            }
        }