Linq to sql 主从表查询

        private void BindProducts(int startRow)
        {
            NorthwindDataContext db = new NorthwindDataContext();

            //var ca = from c in db.Categories
            //         where c.Products.Count > 2
            //         select new
            //         {
            //             id = c.CategoryID,
            //             name = c.CategoryName,
            //             产品数量 = c.Products.Count,
            //             产品 = c.Products.Sum(q => q.UnitPrice * q.UnitsOnOrder)
            //         };


            var ca = from p in db.Products
                           where p.OrderDetails.Count > 2
                           select new
                           {
                               ID = p.ProductID,
                               Name = p.ProductName,
                               NumOrders = p.OrderDetails.Count,
                               Revenue = p.OrderDetails.Sum(o => o.UnitPrice * o.Quantity)
                           };

            GridView1.DataSource = ca.Skip(startRow).Take(10);
            GridView1.DataBind();
        }

posted @ 2008-12-25 10:20  昕友软件开发  阅读(1181)  评论(0编辑  收藏  举报
欢迎访问我的开源项目:xyIM企业即时通讯