OT: Linq Question

OT: Linq Question

Old forum URL: forums.lhotka.net/forums/t/5189.aspx


SomeGuy posted on Friday, August 01, 2008

Ok, working on a simple app using CSLA 3.5 and am having trouble with my Linq queries.

Seemingly simple query: Get number of Orders and total of Order amounts by Customer.

The SQL is straightforward:

SELECT c.Id, c.Name, COUNT(o.Id), SUM(o.Total)
FROM Customer c
LEFT JOIN Orders o ON o.CustomerID = c.Id
WHERE o.OrderDate >= '7/1/2008' AND o.OrderDate <= '7/31/2008'
GROUP BY c.Id, c.Name

How the heck do you do it in Linq? Thanks.

 

SomeGuy replied on Friday, August 01, 2008

Muddled through it myself.

var data = (from o in Orders
                  where o.OrderDate >= '7/1/2008' && o.OrderDate <= '7/31/2008'
                  group o by o.Customer.ID into orderGroup
                  select new { orderGroup.Key, orderCount = orderGroup.Count(), totalAmount = orderGroup.Sum(o => o.Total) })

 

Copyright (c) Marimer LLC