我一直在尝试将以下查询从SQL转换为LINQ,我的查询的主要目的是找到同一个发件人的两个或多个交易,其中金额的总和> = 2000 < / p>
SELECT t.TXN_SENDER_ID,
s.SEN_FIRSTNAME,
s.SEN_LASTNAME,
SUM(t.TXN_AMOUNT)
FROM TRANSACTIONS t
INNER JOIN SENDERS s ON s.SEN_ID = t.TXN_SENDER_ID
WHERE t.TXN_DATE BETWEEN Dateadd(YEAR, -3, Getdate()) AND Getdate()
GROUP BY t.SENDER_ID, s.SENDER_NAME, s.SENDER_LASTNAME
HAVING SUM(t.TXN_AMOUNT) > 2000
以下是我尝试的内容:
var query = from t in context.TRANSACTIONS
join s in context.SENDERS on t.TXN_SENDER_ID equals s.SEN_ID
group new { t, s } by new { t.TXN_SENDER_ID, s.SEN_FIRSTNAME, s.SEN_LASTNAME, t.TXN_AMOUNT } into gr
where (gr.Sum(x => x.t.TXN_AMOUNT) > 1000)
select new { gr.Key.TXN_AMOUNT, gr.Key.SEN_FIRSTNAME, gr.Key.SEN_LASTNAME };
提前致谢
答案 0 :(得分:0)
好的,您还没有回答我的问题,但是查看您希望在TXN_DATE
上过滤数据的SQL查询。我最好的猜测:
DateTime dtFrom = DateTime.Today.AddDays(-3);
DateTime dtTo = DateTime.Today;
var query = from t in context.TRANSACTIONS
.Where(tr=>tr.TXN_DATE>=dtFrom && tr.TXN_DATE<=dtTo)
join s in context.SENDERS on t.TXN_SENDER_ID equals s.SEN_ID
group new { t, s } by new { t.TXN_SENDER_ID, s.SEN_FIRSTNAME, s.SEN_LASTNAME, t.TXN_AMOUNT } into gr
where (gr.Sum(x => x.t.TXN_AMOUNT) >= 2000)
select new
{
gr.Key.TXN_SENDER_ID,
gr.Key.SEN_FIRSTNAME,
gr.Key.SEN_LASTNAME,
gr.Sum(a=>a.TXN_AMOUNT)
};