using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace LinqSqlEpisodeIIIProVid
{
    public partial class _Default : Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            NorthwindDataClassesDataContext northwindContext = new NorthwindDataClassesDataContext();

            int startRow = Convert.ToInt32(Request.QueryString["startRow"]);
            BindProducts(startRow);

            #region Selecting All of the Products
            //var query = from p in northwindContext.Products
            //            where p.CategoryID == 3
            //            select p; 
            #endregion

            #region Shaping our Query Results
            //var query = from p in northwindContext.Products
            //            where p.CategoryID == 3
            //            select new
            //            {
            //                ProductID=p.ProductID,
            //                ProductName=p.ProductName
            //            }; 

            //var query = from p in northwindContext.Products
            //            where p.Category.CategoryName == "Beverages"
            //            select new
            //            {
            //                ProductID = p.ProductID,
            //                ProductName = p.ProductName,
            //                OrderDetails = p.Order_Details.Count,
            //                Revenue = p.Order_Details.Sum(o => o.UnitPrice * o.Quantity)
            //            };
            #endregion

            
        }
        protected void BindProducts(int startRow)
        {
            NorthwindDataClassesDataContext northwindContext = new NorthwindDataClassesDataContext();
            startRow--;
            var query = from p in northwindContext.Products
                        select new
                        {
                            ProductID = p.ProductID,
                            ProductName = p.ProductName,
                            OrderDetails = p.Order_Details.Count,
                            Revenue = p.Order_Details.Sum(o => o.UnitPrice * o.Quantity)
                        };
            GridView1.DataSource = query.Skip(startRow * 5).Take(5);
            GridView1.DataBind();
        }
    }
}