日期:2014-05-17  浏览次数:20448 次

求一条带sum的linq语句
现在有三个表
t_Product 表
id
name

t_Price 表
id
Product_id
price

t_Quantity 表
id
Price_id
Quantity 

我想要的效果是查出商品名、商品价格、商品的销售总量
请问linq语句如何写

------解决方案--------------------
http://blog.sina.com.cn/s/blog_412ec72c0100mall.html
------解决方案--------------------
var query = from p in t_Product
            join r in t_Price on p.id equals r.Product_id
            join q in t_Quantity on r.id equals q.Price_id
            select new { p.id, p.name, r.price, q.Quantity };
------解决方案--------------------
var query = (from p in t_Product
             join r in t_Price on p.id equals r.Product_id
             join q in t_Quantity on r.id equals q.Price_id
             select new { p.id, p.name, r.price, q.Quantity }).GroupBy(x => x.id).
             Select(x => new { id = x.Key, x.First().Name, price = Sum(y => y.price * y.Quantity)})
         
------解决方案--------------------
var query = (from p in t_Product
             join r in t_Price on p.id equals r.Product_id
             join q in t_Quantity on r.id equals q.Price_id
             select new { p.id, p.name, r.price, q.Quantity })
             .GroupBy(p =>new {  p.name, r.price, q.Quantity})
             .OrderByDescending(g=>g.Sum(x=>x.Quantity))
             .Select(g => new { g.Key.Name,g.Key.Price, Quantity= g.Sum(x=>x.Quantity)});