using System;
using System.Linq;
using System.Windows.Forms;
using _2_ORM_Model;
using Telerik.OpenAccess;
using Telerik.OpenAccess.Query;

namespace _2_ORM_MyWinFormApp
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            cbCategories.SelectedIndex = 0;
        }

        private void cbCategories_SelectedIndexChanged(object sender, EventArgs e)
        {
            /* First Example - Before the Data Grid is Place on the Page
               if ((sender as ComboBox).SelectedValue != null)
                 {
                     this.Text = (sender as ComboBox).SelectedValue.ToString();
                 }
            */

            /* - Second Example
            if ((sender as ComboBox).SelectedValue != null)
            {
                IObjectScope scope = NorthwindProvider.ObjectScope();

                int? selectValue = (int?)(sender as ComboBox).SelectedValue;
                var filteredData = from product in scope.Extent<Product>()
                                   where product.CategoryID == selectValue
                                   select product;
                gvProducts.DataSource = filteredData.ToList();
            }
             */

            // Final Example - Filtered List
            if ((sender as ComboBox).SelectedValue != null)
            {
                IObjectScope scope = NorthwindProvider.ObjectScope();

                int? selectValue = (int?)(sender as ComboBox).SelectedValue;
                var filteredData = from product in scope.Extent<Product>()
                                   where product.CategoryID == selectValue
                                   select new
                                   {
                                       Product = product.ProductName,
                                       QuantityPerUnit = product.QuantityPerUnit,
                                       Category = product.Category.CategoryName
                                   };
                gvProducts.DataSource = filteredData.ToList();
            }


        }

    }
}
