0

How can I replicate the following query in Linq?

SELECT 
    A.GroupID,
    COUNT(DISTINCT B.GroupName) AS [Number of SubGroups],
    SUM(B.AmountRaised) AS [Total raised by All Sub Groups]
FROM
    dbo.Group A
    INNER JOIN dbo.SubGroupActivity B
        ON A.ID = B.GroupID
GROUP BY A.GroupID
HAVING SUM(B.AmountRaised) > 0
Stewart Alan
  • 1,521
  • 5
  • 23
  • 45

1 Answers1

0

If I understand your query correct, and you are using LINQ to SQL, here is how I would translate:

var ans = from a in dbo.Group
          join b in dbo.SubGroupActivity on a.GroupID equals b.GroupID
          group new { a, b } by a.GroupID into abg
          let TotalRaised = abg.Sum(ab => ab.b.AmountRaised)
          where TotalRaised > 0
          select new {
            GroupID = abg.Key,
            Number = abg.Select(ab => ab.b.GroupName).Distinct().Count(),
            TotalRaised
          };
NetMage
  • 26,163
  • 3
  • 34
  • 55