我的SQL查询按预期工作:
SELECT SUM(outcome), toaddress
FROM lot l
JOIN incomingpayment ip
ON l.incomingpayment_id = ip.id
WHERE outcome > 0
GROUP BY ip.toaddress
ORDER BY SUM(outcome) DESC
我试图把它变成LINQ语句,但无论如何都没有取得很大的成功:
var result =
from l in session.Query<Lot>().ToList()
join ip in session.Query<IncomingPayment>().ToList()
on l.IncomingPayment equals ip
where l.Outcome > 0
group ip by new {ip.ToAddress}
into g
select new
{
Address = g.Key,
SumAmount = g.Sum(x => x.Outcome)
};
最后一行中的 Outcome
是Lot
字段,因为我正在对IncomingPayment
(group ip...
)进行分组。 g.Sum()
来电时无法使用。
我在这里缺少什么?
答案 0 :(得分:1)
你可以试试这个:
var result = from g in (from l in session.Query<Lot>().ToList()
join ip in session.Query<IncomingPayment>().ToList()
on l.IncomingPayment equals ip.Id
where l.Outcome > 0
select new { Address = ip.ToToAddress, Outcome = l.Outcome})
group g by g.Address
select new
{
Address = g.Key,
SumAmount = g.Sum(x => x.Outcome)
};
或更明确地说:
Lots = session.Query<Lot>().ToList();
IncomingPayments = session.Query<IncomingPayment>().ToList();
var result = Lots.Join(IncomingPayments,
x=>x.IncomingPayment,
y=>y.Id,
(x,y) => new
{
Address = x.ToToAddress,
OutCome = y.Outcome
}).GroupBy(x => x.Address,
x => x.Outcome,
(kew, group) =>
{
Address = key,
SumAmount = group.Sum()
});