As the name says, my goal is to join two tables (goal and transaction) to multiple columns, and then group the result of this join and sum the column values from the BOTH tables. The following query allows access to columns from the FIRST table in a join!
var actualsVsTargets = (from target in ObjectContext.PipelineTargets join transaction in ObjectContext.Transactions on new { target.Year, target.Quarter, target.StateID, target.ProductGroup.TeamId } equals new { transaction.Year, transaction.Quarter, transaction.StateID, transaction.Product.ProductGroup.TeamId } where target.Year == year && target.ProductGroup.TeamId == teamId group target by new { target.ProductGroupID, target.StateID, target.Year } into targetGroup select new {
As shown below, this is trivial to implement in T-SQL (approximately):
SELECT targets.Year, targets.StateID, SUM(targets.Level1_Target) L1_Target, -- get the sum of targets SUM(transactions.Level1_Total) L1_Total -- get the sum of transactions FROM PipelineTargets targets JOIN Transactions transactions JOIN Products prods ON transactions.ProductID = prods.ProductID ON targets.Year = transactions.Year and targets.Quarter = transactions.Quarter and targets.StateID = transactions.StateID and prods.ProductGroupID = targets.ProductGroupID WHERE targets.Year = '2010' and targets.StateID = 1 GROUP BY targets.Year, targets.StateID, targets.ProductGroupID
How to do it in LINQ?
source share