I have 3 tables :
order : corresponds to customer orders
order_detail : corresponds to line of customer orders
order_detail_commission : corresponds to commissions earned by commercial agent
third_party_agent : corresponds to commercial agents
I would like to retrieve the total amount of commissions earned by commercial agents by order.
In SQL, I would write it like this :
select SUM(order_detail_commission.CommissionRate * order_detail.Price), third_party_agent.Name
from order_detail
join order_detail_commission on order_detail_commission.FK_order_detail_Id = order_detail.Id
join third_party_agent on third_party_agent.Id = order_detail_commission.FK_third_party_agent_Id
GROUP BY third_party_agent.Id
In Linq, I tried to write something like this :
var query =
from orderDetail in metaData.OrderDetail
join orderDetailCommission in metaData.OrderDetailCommission on orderDetail.Id equals orderDetailCommission.FkOrderDetailId
join thirdPartyAgent in metaData.ThirdPartyAgent on orderDetailCommission.FkThirdPartyAgentId equals thirdPartyAgent.Id
group orderDetailCommission by orderDetailCommission.FkThirdPartyAgentId into g
select new DeliveryAgentCommissionSummaryProjection
{
Id = g.Key,
CommissionAmount = g.Sum(i => i.OrderDetail.Quantity * i.CommissionRate * i.OrderDetail.Price),
ThirdPartyAgentText = "Test"
};
which generate the following sql query :
SELECT `LPA_L1`.`FkThirdPartyAgentId` AS `Id`, `LPA_L1`.`LPAV_` AS `CommissionAmount`, 'Test' AS `LPFA_10`
FROM
(
SELECT `LPA_L3`.`FkThirdPartyAgentId`, SUM(`LPA_L3`.`LPAV_`) AS `LPAV_`
FROM
(
SELECT `LPA_L5`.`FK_third_party_agent_Id` AS `FkThirdPartyAgentId`, (`LPLA_7`.`Quantity` * `LPLA_5`.`CommissionRate`) * `LPLA_7`.`Price` AS `LPAV_`
FROM
(
(
(
`order_detail` `LPA_L4` INNER JOIN `order_detail_commission` `LPA_L5` ON `LPA_L4`.`Id` = `LPA_L5`.`FK_order_detail_Id`
)
INNER JOIN `third_party_agent` `LPA_L7` ON `LPA_L5`.`FK_third_party_agent_Id` = `LPA_L7`.`Id`
)
INNER JOIN `third_party` `LPA_L6` ON `LPA_L6`.`Id`=`LPA_L7`.`Id`)
) `LPA_L3`
GROUP BY `LPA_L3`.`FkThirdPartyAgentId`
) `LPA_L1
`
But the following error is thrown :
An exception was caught during the execution of a retrieval query: Unknown column 'LPLA_7.Quantity' in 'field list'. Check InnerException, QueryExecuted and Parameters of this exception to examine the cause of this exception.
It's normal because the Quantity column doesn't exist in third_party_agent but in order_detail.
So I tried to split the Linq query in 2. In the first query, I create an anonymous type containing all the columns I need then I apply the group by clause on this anonymous type like this :
var tempoQuery =
from orderDetail in metaData.OrderDetail
join order in metaData.Order on orderDetail.FkOrderId equals order.Id
join orderDetailCommission in metaData.OrderDetailCommission on orderDetail.Id equals orderDetailCommission.FkOrderDetailId
join thirdPartyAgent in metaData.ThirdPartyAgent on orderDetailCommission.FkThirdPartyAgentId equals thirdPartyAgent.Id
select new
{
FkThirdPartyAgentId = thirdPartyAgent.FkThirdPartyAgentId,
ThirdPartyAgentText = thirdPartyAgent.Name,
Quantity = orderDetail.Quantity,
CommissionRate = orderDetailCommission.CommissionRate,
Price = orderDetail.Price
};
var query =
from deliveryAgentCommission in tempoQuery
group deliveryAgentCommission by deliveryAgentCommission.FkThirdPartyAgentId into g
select new DeliveryAgentCommissionSummaryProjection
{
Id = g.Key,
ThirdPartyAgentId = g.Key,
ThirdPartyAgentText = ??????,
CommissionAmount = g.Sum(i => i.Quantity * i.CommissionRate * i.Price)
};
But I don't know how to retrieve the name of the agent (third_party_agent.Name).
Please help me