gpt4 book ai didi

.net - 使用 group by 和聚合函数的简单 sql 到 Linq 查询

转载 作者:行者123 更新时间:2023-12-04 03:02:31 24 4
gpt4 key购买 nike

我正在与 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 - 使用 group by 和聚合函数的简单 sql 到 Linq 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3955986/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com