0

我的 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最后一行是 aLot的字段,由于我正在分组IncomingPayment( ),因此在通话中group ip...不可用。g.Sum()

我在这里想念什么?

4

1 回答 1

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()
                                  });    
于 2014-06-26T12:49:23.120 回答