coco coco - 1 year ago 84
SQL Question

Changing a SQL query to a linq query - syntax

I'm having problem coding my linq query.

This is my SQL query:

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

This is my linq query:

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)));

This linq query seems to be incorrect. What is the right syntax?

Answer Source

This should work: (You need to move the order part to be after the grouping)

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);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download