我正在尝试使用linq来学习语法,但我不知道如何执行以下简单查询
SELECT DISTINCT
user.firstname,
user.lastname,
COUNT(invoice.amount),
SUM(invoice.amount)
FROM
company_user
INNER JOIN
user
ON
company_user.user_id = user.user_id
INNER JOIN
invoice
ON
invoice.user_id= invoice.user_id
WHERE
company_user.company_id = 1
GROUP BY
user.firstname,
user.lastname,
GO
将其转换为linq的任何帮助都会很棒。
最佳答案
您要查询的内容应与此非常接近:
var query =
from cu in company_user
where cu.company_id == 1
join u in user on cu.user_id equals u.user_id
join i in invoice on u.user_id equals i.user_id
group i.amount by new
{
u.firstname,
u.lastname,
} into gs
select new
{
firstname = gs.Key.firstname,
lastname = gs.Key.lastname,
count = gs.Count(),
sum = gs.Sum(),
};
享受!
关于.net - 简单的SQL到Linq查询,具有分组和汇总功能,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/3955986/