2016-07-08 72 views
-2

我遇到了编码我的linq查询的问题。将SQL查询更改为linq查询 - 语法

这是我的SQL查询:

select 
    price, (cast(sum(Quantity) as decimal(7,2))) 
from 
    OrderDetails 
where 
    ItemID = 1000 
group by 
    price 
order by 
    price 

这是我的LINQ查询:

var result = from od in db.OrderDetails 
      where od.ItemID == 1000 
      orderby od.Price 
      group by price 
      select od.price, (cast(sum(od.Quantity) as decimal(7, 2))); 

这LINQ查询似乎是不正确的。什么是正确的语法?

回答

1

这应该工作: (你需要移动的顺序部分是分组后)

var q = (from o in context.OrderDetails 
      where o.ItemID == 1000 
      group o by o.price into grp 
      select new 
      { 
       Price = grp.Key, 
       Quantity = grp.Sum(x => x.Quantity) 
      }).OrderBy(a => a.Price); 
+1

它不工作。 – coco

+1

它的工作原理。谢谢!!! – coco